/*
 * Created on 06-May-2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package wstrader.task.trade.physical;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import wstrader.task.GenReport;
import wstrader.task.trade.TradeTask;
import pc.sql.value.*;
import pc.system.*;
import pc.util.Utils;
/**
 * @author NANSA
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Contract extends TradeTask
{    
	  public Contract(Tasks owner)
	  {
	    super(owner);
	  }

	  public long doTask()
	  {
		int RecId =0;
		String contract_price_type="";
		float fp=0;
		long focused_id=0;	  	
	  	
	    int task_id = this.getRequest().getIntValue("task_id");
	    String Type = this.getRequest().getStringValue("type");
        focused_id=this.getRequest().getIntValue("contracts");
        int GradeId = this.getRequest().getIntValue("grade_id");
        
	    contract_price_type = this.getRequest().getStringValue("contract_price_type");
	    
	    fp = getRequest().getFloatValue("contract_price_foreign");
	    RecId=this.getRequest().getIntValue("rec_id");

	    switch (task_id)
		{
	      case 1 : // Update
	      if (RecId > 0)
	      {
	        focused_id = doUpdate("contracts",focused_id);
	        if (Type.compareTo("P")==0)
	        {
	        	float sumValue = getReceivedTons(focused_id);//getSum("tons_delivered","purchase_contract_id","purchase_delivery_details",focused_id);

	        	int Ret = setFieldValue(focused_id,"receive_tons"," "+sumValue,"contracts");
	    	
	        	Ret = setCalculateFieldValue(focused_id,"pending_tons","tons","receive_tons","-","contracts");
	    	
	        	if (contract_price_type.compareTo("U")==0)
	        	{
	        		float sum_tons_fixed = getSum("tons_fixed","contract_id","contract_price_fixation",focused_id);
	        		Ret = setFieldValue(focused_id,"fixed_tons"," "+sum_tons_fixed,"contracts");
	        	
	        		Ret = setCalculateFieldValue(focused_id,"unfixed_tons","tons","fixed_tons","-","contracts");
	        	
	        		float sum_hedge_price = getSum("hedge_price * tons_fixed","contract_id","contract_price_fixation",focused_id);
	        	
	        		float hedge_price=0;
	        		if (sum_tons_fixed > 0)
	        		{
	        			hedge_price = (sum_hedge_price/sum_tons_fixed);
	        		}
	        		else
	        		{
	        			hedge_price = 0;
	        		}
	        	
	        		float sum_price = getSum("price_foreign * tons_fixed","contract_id","contract_price_fixation",focused_id);
	        	
	        		float price = 0;
	        		if (sum_tons_fixed > 0)
	        		{
	        			price = (sum_price/sum_tons_fixed);
	        		}
	        		else
	        		{
	        			price=0;
	        		}
	        		Ret = setFieldValue(focused_id,"hedge_price"," "+hedge_price,"contracts");
	        	
	        		Ret = setFieldValue(focused_id,"contract_price_foreign"," "+price,"contracts");
	        		//updateRollOver(focused_id);
	        	}
	        	else
	        	{
	        		Ret = equateFieldValue(focused_id,"fixed_tons","tons","contracts");
	        		Ret = setFieldValue(focused_id,"unfixed_tons"," "+0,"contracts");
	        	}
	        	updatedPosRptField(focused_id);
	        	//updatebasisPriceField(focused_id);
	        	//updateContractDiscAllowDts(focused_id,GradeId);
	        }	
	        else
	        if (Type.compareTo("S")==0)
	        {
	            int Ret=0;
	        	float sumValue = getInvoicedTons(focused_id);//getSum("tons_delivered","purchase_contract_id","purchase_delivery_details",focused_id);
	        	Ret = setFieldValue(focused_id,"inv_tons"," "+sumValue,"contracts");
	        	
	            if (contract_price_type.compareTo("U")==0)
	            {
	            	float sum_tons_fixed = getSum("tons_fixed","contract_id","contract_price_fixation",focused_id);
	            	
	            	Ret = setFieldValue(focused_id,"fixed_tons"," "+sum_tons_fixed,"contracts");
	            	
	            	Ret = setCalculateFieldValue(focused_id,"unfixed_tons","tons","fixed_tons","-","contracts");
	            	
	            	float sum_hedge_price = getSum("hedge_price * tons_fixed","contract_id","contract_price_fixation",focused_id);
	            	float hedge_price = 0;
	            	if (sum_tons_fixed > 0)
	            	{
	            		hedge_price = (sum_hedge_price/sum_tons_fixed);
	            	}
	            	float sum_price = getSum("price_foreign * tons_fixed","contract_id","contract_price_fixation",focused_id);
	            	float price =0;
	            	if (sum_tons_fixed > 0)
	            	{
	            		price = (sum_price/sum_tons_fixed);
	            	}
	            	Ret = setFieldValue(focused_id,"hedge_price"," "+hedge_price,"contracts");
	            	
	            	Ret = setFieldValue(focused_id,"contract_price_foreign"," "+price,"contracts");
	            	
	            	updateRollOver(focused_id);
	            }
	            else
	            {
	            	Ret = equateFieldValue(focused_id,"fixed_tons","tons","contracts");
	            }
	          	updatebasisPriceField(focused_id);	        	
	        }
	        
	      }
	      break;
	      case 2 : // Delete
//	        focused_id = doDelete(table_name,focused_id);
	        focused_id = 1;
	        break;
	      case 5: //Report
		      if (RecId > 0)
		      {	      	
		      		String ComName = getUser().getComanyName().toUpperCase();
		      		String CPType = getRequest().getStringValue("contract_price_type");
		      		if (ComName.compareTo("MERCAFE VIETNAM")==0)
		      		{
		      			if (Type.compareTo("P")==0)
		      			{
		      				if (CPType.compareTo("F")==0) getResponse().setReport(new PrintMercafeContract(this).generate_Mercafe_FixPurCnt());
		      				if (CPType.compareTo("U")==0) getResponse().setReport(new PrintMercafeUnfixedContract(this).generate_Mercafe_UnFixPurCnt());
		      			}
		      			
		      			if (Type.compareTo("S")==0)
		      			{
		      				//getResponse().setReport(new PrintMerconSalesContract(this).generate_mercon());
		      			}		      			
		      		}
		      		else
			      		if (ComName.compareTo("MERCON COFFEE CORP.")==0)
			      		{
			      			if (Type.compareTo("P")==0)
			      			{
			      				getResponse().setReport(new PrintMerconContract(this).generate_mercon());
			      			}
			      			
			      			if (Type.compareTo("S")==0)
			      			{
			      				getResponse().setReport(new PrintMerconSalesContract(this).generate_mercon());
			      			}		      			
			      		}		      			
			      		else
			      			if (ComName.compareTo("VINACOF")==0)
			      			{
				      			if (Type.compareTo("P")==0)
				      			{
				      				if (CPType.compareTo("F")==0) getResponse().setReport(new PrintVinacofPurContract(this).generate_vinacof());
				      				if (CPType.compareTo("U")==0) getResponse().setReport(new PrintVinacofUnfixPurContract(this).generate_vinacof());
				      			}
				      			
				      			if (Type.compareTo("S")==0)
				      			{
				      				if (CPType.compareTo("F")==0) getResponse().setReport(new PrintVinacofSalContract(this).generate_vinacof());
				      				if (CPType.compareTo("U")==0) getResponse().setReport(new PrintVinacofUnfixSalContract(this).generate_vinacof());
				      			}		      						      				
			      			}
			      			else
			      			{
			      				if (Type.compareTo("P")==0) getResponse().setReport(new PrintPurContract(this).generate());
			      				if (Type.compareTo("S")==0) getResponse().setReport(new PrintSalContract(this).generate());
			      			}
		      }
	      	break;
	      	
	      	case 6:
	      		updatePaymentDetails();
	      	break;
	      	
	      	case 7:
	      		float UFTon = getRequest().getFloatValue("unfixed_tons");
	      		if (getUser().getComanyName().toUpperCase().compareTo("MERCAFE VIETNAM")==0)
	      		{
	      			if (UFTon == 0)
	      			{
	      				getResponse().setReport(new PrintMercafePriceConfirmationFixed(this).generate_Mercafe_PriceConfirmationFixed());
	      			}
	      			if (UFTon > 0)
	      			{
	      				getResponse().setReport(new PrintMercafePriceConfirmationUnFixedPart(this).generate_Mercafe_PriceConfirmationUFPart());
	      			}
	      		}
	      		break;
	      		
	      	case 8:
	      		float UFT = getRequest().getFloatValue("unfixed_tons");
	      		if (getUser().getComanyName().toUpperCase().compareTo("MERCAFE VIETNAM")==0)
	      		{
	      			if (UFT == 0)
	      			{
	      				getResponse().setReport(new PrintMercafePriceConfirmationUnFixedFinal(this).generate_Mercafe_PriceConfirmationUFFinal());
	      			}
	      		}
	      		break;	      		
	        default : //View only
	        break;
	      
	    }
	    
	    this.setFocusedID(focused_id);
	    return focused_id;
	  }	 
	  

	  
	  public ResultSet getBrokerNo()
	  {
	  	return getSQLCommand().executeQuery("select contract_no from contracts where active = 1 order by id desc");
	  }
	  
	  public ResultSet fetchContractList()
	  {
	  
	  	String CntType = getRequest().getStringValue("cnt_type");
	  	if (CntType.compareTo("")== 0) CntType = "0";
	  	int OriginId = getRequest().getIntValue("origin");
	 	int GradeId = getRequest().getIntValue("grade");	  	
	  	int BuySellId = getRequest().getIntValue("buyer_seller");
	  	java.sql.Date LsDt = getRequest().getValueDate("ls_date");
	  	//Utils.strToDate(getRequest().getStringValue("ter_mth"));
	  	String Status = getRequest().getStringValue("status");
	  	int QualityId = getRequest().getIntValue("quality");
	  	if (Status.compareTo("")== 0) Status = "0";
	  	String Sql="";
	  	if (Status.compareTo("0")==0)
	  	{
	  		Sql ="Select * from contracts where " +
	  				"(type = ? OR ?) " +
	  		        "and (origin_id = ? OR ?) "+
					"and (grade_id = ? OR ?) " +
					"and (supplier_buyer_id = ? OR ?) " +
					"and (quality_id = ? OR ?) " +
	  				"and (last_date <= ? OR ?) " +
					"and active = 1 order by id desc";
	  		Vector vp = new Vector();

	  		vp.addElement(new StringValue(CntType));
	  		vp.addElement(new BooleanValue(CntType.compareTo("0") == 0));

	  		vp.addElement(new IntValue(OriginId));
	  		vp.addElement(new BooleanValue(OriginId == 0));

	  		vp.addElement(new IntValue(GradeId));
	  		vp.addElement(new BooleanValue(GradeId == 0));

	  		vp.addElement(new IntValue(BuySellId));
	  		vp.addElement(new BooleanValue(BuySellId == 0));

	  		vp.addElement(new IntValue(QualityId));
	  		vp.addElement(new BooleanValue(QualityId == 0));	  		
	  		
	  		vp.addElement(new DateValue(LsDt));
	  		vp.addElement(new BooleanValue(LsDt.compareTo(new java.sql.Date(0)) == 0));
	  	
	  		return getSQLCommand().executeQuery(Sql,vp);	  	
	  	}
	  	else
	  	{
	  		if (Status.compareTo("F")==0)
	  		{
	  			Sql ="Select * from contracts where " +
	  				"(type = ? OR ?) " +
	  		        "and (origin_id = ? OR ?) "+
					"and (grade_id = ? OR ?) " +
					"and (supplier_buyer_id = ? OR ?) " +
					"and (quality_id = ? OR ?) " +
	  				"and ((last_date <= ? OR ?) OR (last_date IS NULL)) " +
					"and (unfixed_tons=?) order by id desc";
	  		}	
	  		if (Status.compareTo("U")==0)
	  		{
	  			Sql ="Select * from contracts where " +
	  				"(type = ? OR ?) " +
	  		        "and (origin_id = ? OR ?) "+
					"and (grade_id = ? OR ?) " +
					"and (supplier_buyer_id = ? OR ?) " +
					"and (quality_id = ? OR ?) " +
	  				"and ((last_date <= ? OR ?) OR (last_date IS NULL)) " +
					"and (unfixed_tons>?) order by id desc";
	  		}
	  		if (Status.compareTo("P")==0)
	  		{
	  			Sql ="Select * from contracts where " +
	  				"(type = ? OR ?) " +
	  		        "and (origin_id = ? OR ?) "+
					"and (grade_id = ? OR ?) " +
					"and (supplier_buyer_id = ? OR ?) " +
					"and (quality_id = ? OR ?) " +
	  				"and ((last_date <= ? OR ?) OR (last_date IS NULL)) " +
					"and (pending_tons>?) order by id desc";
	  		}
	  		if (Status.compareTo("D")==0)
	  		{
	  			Sql ="Select * from contracts where " +
	  				"(type = ? OR ?) " +
	  		        "and (origin_id = ? OR ?) "+
					"and (grade_id = ? OR ?) " +
					"and (supplier_buyer_id = ? OR ?) " +
					"and (quality_id = ? OR ?) " +
	  				"and ((last_date <= ? OR ?) OR (last_date IS NULL)) " +
					"and (pending_tons=?) order by id desc";
	  		}	  	
	  		Vector vp = new Vector();

	  		vp.addElement(new StringValue(CntType));
	  		vp.addElement(new BooleanValue(CntType.compareTo("0") == 0));

	  		vp.addElement(new IntValue(OriginId));
	  		vp.addElement(new BooleanValue(OriginId == 0));

	  		vp.addElement(new IntValue(GradeId));
	  		vp.addElement(new BooleanValue(GradeId == 0));

	  		vp.addElement(new IntValue(BuySellId));
	  		vp.addElement(new BooleanValue(BuySellId == 0));

	  		vp.addElement(new IntValue(QualityId));
	  		vp.addElement(new BooleanValue(QualityId == 0));	  			  		
	  		
	  		vp.addElement(new DateValue(LsDt));
	  		vp.addElement(new BooleanValue(LsDt.compareTo(new java.sql.Date(0)) == 0));
	  	
	  		vp.addElement(new FloatValue(0));
	  	return getSQLCommand().executeQuery(Sql,vp);
	  	}
	  }	

	  
        private String getNew_DoRef()
        {
          String sql =
              "SELECT MAX(ref_number) as ref_number FROM delivery_order ";
          Record rc = new Record(this.getSQLCommand().executeQuery(sql));
          String ref = (!rc.isEmpty()) ? rc.getString("ref_number") : "DO 00-00000";
          rc.close();
          return "DO" + " " + this.getNextRefNumber(ref);
        }  
        //inserted by nara - ends
        
        private void updatebasisPriceField(long id)
        {
      	java.sql.Date Tdt =Utils.strToDate(getRequest().getStringValue("contract_date"));
      	java.sql.Date SLdt =Utils.strToDate(getRequest().getStringValue("last_date"));
      	java.sql.Date TerMth=Utils.strToDate(getRequest().getStringValue("terminal_month"));
      	String TerMrk =getRequest().getStringValue("terminal_market");
      	String OfferType =getRequest().getStringValue("contract_price_type");
      	float OfferPrice =getRequest().getFloatValue("contract_price_foreign");
      	Diff =getRequest().getFloatValue("differential");

      	//GETTING TERMINAL PRICE
        	String sql = "SELECT basis_price FROM futures_daily_basis where terminal_month = '" + TerMth + "' and terminal_market = '" + TerMrk + "' and daily_basis_date = '" + Tdt + "'";
        	ResultSet Rs = getSQLCommand().executeQuery(sql);	
        	float bp = 0;
        	try {
      		Rs.beforeFirst();
      	  	if (Rs.next()==true)
      	  	{
      	  		bp= Rs.getFloat("basis_price");
      	  	}
      	  	else
      	  	{
      	  		bp =0;
      	  	}
      	  	System.out.println("BP:-" + bp);
      	  	Rs.close();
      	  	//GETTING TERMINAL PRICE
      	  	
      		float diff = getDiff();
      		
      	  	Rs = getSQLCommand().executeQuery("select month from terminal_month_master where type = '" + TerMrk + "' and month > '" + SLdt + "' order by month");
      	  	java.sql.Date Basis_TDt=null;
      		Rs.beforeFirst();
      		if (Rs.next()==true)
      		{
      			if (SLdt==Basis_TDt)
      			{
      				if (Rs.next()==true)
      				{
      					Rs.next();
      					Basis_TDt = Rs.getDate("Month");
      				}
      				else
      				{
      					Basis_TDt = Rs.getDate("Month");
      				}
      			}
      			else
      			{
      				Basis_TDt = Rs.getDate("Month");
      			}
      			Rs.close();
      		}	  	
      	  	sql = "SELECT terminal_month,closing_price,basis_price FROM futures_daily_basis where terminal_month = '" + Basis_TDt + "' and terminal_market = '" + TerMrk + "' and daily_basis_date = '" + Tdt + "'";
      	  	ResultSet rstp = getSQLCommand().executeQuery(sql);
      	  	
      		rstp.beforeFirst();
      		
      		float cls_price = 0;
      		float bas_price = 0;
      		String bas_termth = "";
      		float bas_diff=0;
      		float bas_outright =0;
      		if (rstp.next()==true)
      		{
      			cls_price = rstp.getFloat("closing_price");
      			bas_price = rstp.getFloat("basis_price");
      			bas_termth = Utils.dateToStr(rstp.getDate("terminal_month")).substring(3);
      			
      			if (OfferType.compareTo("U")==0)
      			{
      				bas_diff =  ((bp-bas_price)+(diff));
      			}
      			else
      			{
      				Diff = OfferPrice-bp;
      				bas_diff =  (OfferPrice-bas_price);
      			}
      			
      			bas_outright =  (bas_price + bas_diff);
      		}
      	  	
      		sql = "update contracts set " +
      			  "basis_terminal_month = ?, " +
      			  "basis_terminal_market = ?, " +
      			  "basis_price = ?, " +
      			  "basis_outright = ?, " +
      			  "basis_diff = ? where id = ?";
      		
      	 	Vector vp = new Vector();

      	 	vp.addElement(new DateValue(Basis_TDt));
      	 	vp.addElement(new StringValue(TerMrk));
      	 	vp.addElement(new FloatValue(bas_price));
      	 	vp.addElement(new FloatValue(bas_outright));
      	 	vp.addElement(new FloatValue(bas_diff));
      	 	vp.addElement(new LongValue(id));
      	 	int i= getSQLCommand().executeUpdate(sql,vp);
      	 	
      	} catch (SQLException e) {
      		// TODO Auto-generated catch block
      		e.printStackTrace();
      	}	
        }
        
        private void updateDeliveryOrder(long PcntId)
        {
        	String DoRef = this.getNew_DoRef();
        	java.sql.Date CntDt = Utils.strToDate(getRequest().getStringValue("contract_date"));
        	int SupplierId = getRequest().getIntValue("supplier_id");
        	int QualityId = getRequest().getIntValue("quality_id");
        	int GradeId = getRequest().getIntValue("grade_id");
        	String Desc = getRequest().getStringValue("description");
        	int PackingId = getRequest().getIntValue("packing_id");
        	java.sql.Date FrmDt = Utils.strToDate(getRequest().getStringValue("first_date"));
        	java.sql.Date ToDt = Utils.strToDate(getRequest().getStringValue("last_date"));
        	float NoOfBags = getRequest().getFloatValue("no_of_bags");
        	float Tons = getRequest().getFloatValue("tons");
        	long RecExist = this.findID("delivery_order","contract_id",PcntId);
       
        	String SqlStr="";
        	if (RecExist > 0)
        	{
       		SqlStr ="update delivery_order " +
      		"SET ref_number = ?, " +
      		"do_date = ?, " +
      		"contract_id = ?, " +
      		"supplier_id = ?, " +
      		"quality_id = ?, " +
      		"grade_id = ?, " +
      		"description = ?, " +
      		"packing_id = ?, " +
      		"date_from = ?, " +
      		"date_to = ?, " +
      		"no_of_bags = ?, " +
      		"tons = ? where id = ?";	
        		Vector vp = new Vector();
        		vp.addElement(new StringValue(DoRef));
        		vp.addElement(new DateValue(CntDt));
        		vp.addElement(new LongValue(PcntId));
        		vp.addElement(new IntValue(SupplierId));
        		vp.addElement(new IntValue(QualityId));
        		vp.addElement(new IntValue(GradeId));
        		vp.addElement(new StringValue(Desc));
        		vp.addElement(new IntValue(PackingId));
        		vp.addElement(new DateValue(FrmDt));
        		vp.addElement(new DateValue(ToDt));
        		vp.addElement(new FloatValue(NoOfBags));
        		vp.addElement(new FloatValue(Tons));
        		vp.addElement(new LongValue(PcntId));
        		int ret = getSQLCommand().executeUpdate(SqlStr,vp);	  		
        	}
        	else
        	{
        		SqlStr ="insert delivery_order " +
      			"SET ref_number = ?, " +
      			"do_date = ?, " +
      			"contract_id = ?, " +
      			"supplier_id = ?, " +
      			"quality_id = ?, " +
      			"grade_id = ?, " +
      			"description = ?, " +
      			"packing_id = ?, " +
      			"date_from = ?, " +
      			"date_to = ?, " +
      			"no_of_bags = ?, " +
      			"tons = ?";
        			Vector vp = new Vector();
        			vp.addElement(new StringValue(DoRef));
        			vp.addElement(new DateValue(CntDt));
        			vp.addElement(new LongValue(PcntId));
        			vp.addElement(new IntValue(SupplierId));
        			vp.addElement(new IntValue(QualityId));
        			vp.addElement(new IntValue(GradeId));
        			vp.addElement(new StringValue(Desc));
        			vp.addElement(new IntValue(PackingId));
        			vp.addElement(new DateValue(FrmDt));
        			vp.addElement(new DateValue(ToDt));
        			vp.addElement(new FloatValue(NoOfBags));
        			vp.addElement(new FloatValue(Tons));
        			int ret = getSQLCommand().executeUpdate(SqlStr,vp);	  		
        	}

        }  
        
        private void updateRollOver(long id)
        {
     	  	java.sql.Date RollDt = Utils.strToDate(getRequest().getStringValue("contract_date"));
    	  	java.sql.Date RollTerMth = Utils.strToDate(getRequest().getStringValue("terminal_month"));
    	  	float LotsRolled = getRequest().getFloatValue("no_of_lots");
    	  	float TonsRolled = getRequest().getFloatValue("tons");
    	  	float NewDiff = getRequest().getFloatValue("differential");
    	  	
    	  	java.sql.Date TerMth = Utils.strToDate(getRequest().getStringValue("terminal_month"));
    	  	float Diff = getRequest().getFloatValue("differential");

    	  	long RecCount = getCount("contract_roll_over","contract_id",id);
    	  	if(RecCount==0)
    	  	{
    	  	String SqlStr = "";
    	  		SqlStr =
    			"insert contract_roll_over " +
    			"SET rolling_date = ?, " +
    			"contract_id = ?, " +
    			"tons_rolled = ?, " +
    			"lots_rolled = ?, " +
    			"old_diff = ?, " +
    			"new_diff = ?, " +
    			"old_terminal_month = ?, " +
    			"new_terminal_month = ?";

    			Vector vp = new Vector();
    			vp.addElement(new DateValue(RollDt));
    			vp.addElement(new LongValue(id));
    			vp.addElement(new FloatValue(TonsRolled));
    			vp.addElement(new FloatValue(LotsRolled));
    			vp.addElement(new FloatValue(Diff));
    			vp.addElement(new FloatValue(NewDiff));
    			vp.addElement(new DateValue(TerMth));
    			vp.addElement(new DateValue(RollTerMth));
    			int ret = getSQLCommand().executeUpdate(SqlStr,vp);
    			
    			SqlStr = "Update contracts set terminal_month = ?, " +
    			         "differential = ? " +
    					 "where id = ?";
    			vp = new Vector();
    			vp.addElement(new DateValue(RollTerMth));
    			vp.addElement(new FloatValue(NewDiff));
    			vp.addElement(new LongValue(id));
    			ret = getSQLCommand().executeUpdate(SqlStr,vp);
    	  	}
    	  	if(RecCount==1)
    	  	{
    		  	String SqlStr = "";
    	  		SqlStr =
    			"update contract_roll_over " +
    			"SET rolling_date = ?, " +
    			"contract_id = ?, " +
    			"tons_rolled = ?, " +
    			"lots_rolled = ?, " +
    			"old_diff = ?, " +
    			"new_diff = ?, " +
    			"old_terminal_month = ?, " +
    			"new_terminal_month = ? " +
    			"where contract_id = ?";

    			Vector vp = new Vector();
    			vp.addElement(new DateValue(RollDt));
    			vp.addElement(new LongValue(id));
    			vp.addElement(new FloatValue(TonsRolled));
    			vp.addElement(new FloatValue(LotsRolled));
    			vp.addElement(new FloatValue(Diff));
    			vp.addElement(new FloatValue(NewDiff));
    			vp.addElement(new DateValue(TerMth));
    			vp.addElement(new DateValue(RollTerMth));
    			vp.addElement(new LongValue(id));
    			int ret = getSQLCommand().executeUpdate(SqlStr,vp);
    			
    	  	}
        }
        
        public String getNew_CntRef()
        {
          String sql = "SELECT MAX(ref_number) as ref_number FROM contracts";
          Record rc = new Record(this.getSQLCommand().executeQuery(sql));
          String ref = (!rc.isEmpty()) ? rc.getString("ref_number") : "SWCVN-00-00000";
          rc.close();
          return "SWCVN" + "-" + this.getNextRefNumber(ref);
        } 
        

        public ResultSet getWarehouseRefList()
        {
        	int CntId = getRequest().getIntValue("contracts");
        	String Sql="";
       		Sql ="SELECT delivery_instruction.contract_id,warehouse_receipt.ref_number,warehouse_receipt.id,warehouse_receipt.qr_id " +
			     "FROM warehouse_receipt INNER JOIN delivery_instruction ON (warehouse_receipt.inst_id=delivery_instruction.id) " +
				 "where delivery_instruction.contract_id = " + CntId;
    	  	return getSQLCommand().executeQuery(Sql);        	
        }        
        
        public ResultSet getWhseReceiptQualityReport()
        {
        	int WhseQtyId = getRequest().getIntValue("warehouse_receipt");
           	String Sql = "Select * from quality_report where id = " + WhseQtyId ;
        	return getSQLCommand().executeQuery(Sql);        	
        }
        
        public ResultSet getWhseReceiptDetails()
        {
        	String WhseQtyId = getRequest().getStringValue("warehouse_receipt");
           	//String Sql = "Select * from warehouse_receipt where qr_id = '" + WhseQtyId + "'";
        	String Sql = "SELECT warehouse_receipt.date,sum(weight_note.net_weight) AS net_weight, " +
						 "warehouse_receipt.ref_number, warehouse_receipt.id " +
						 "FROM warehouse_receipt " +
						 "INNER JOIN weight_note ON (warehouse_receipt.id=weight_note.wrc_id) " +
						 "WHERE warehouse_receipt.qr_id = " + WhseQtyId + " " + 
						 "GROUP BY warehouse_receipt.date,warehouse_receipt.ref_number, " +
						 "warehouse_receipt.id";
        	return getSQLCommand().executeQuery(Sql);        	
        }
        
        public ResultSet getContractDiscAllowDetails()
        {
        	int ContractId = getRequest().getIntValue("contracts");
        	String Sql = "Select * from contract_discount_allowance_details where contract_id = " + ContractId;
        	return getSQLCommand().executeQuery(Sql);        	
        }
        
        public ResultSet getPaymentDetails()
        {
        	String QRRefNo = getRequest().getStringValue("warehouse_receipt");
        	String Sql = "Select * from payment_details where qr_id = " + QRRefNo ;
         	return getSQLCommand().executeQuery(Sql);        	
        }
        
        private int updateContractDiscAllowDts(long id,int GradeId)
        {
        	int ret =0;
        	ResultSet Rs = null;
        	String Sql = "";
        	String ISql = "";
			try {        	
        	Sql = "select * from grade_discount_allowance_details where grade_master_id = " + GradeId;
        	Rs = getSQLCommand().executeQuery(Sql);
        	Rs.beforeFirst();
        	
        	if (Rs.next()==true)
        	{	
        		long RecExist = this.findID("contract_discount_allowance_details","contract_id",id);
        		System.out.println("RecExist:-" + RecExist);
        		if (RecExist > 0)
        		{
        			ISql = "Update contract_discount_allowance_details " +
					"SET contract_id = ?, " +
					"grade_master_id = ?, " +
					"black_discount = ?, " +
					"black_allowance = ?, " +					
					"black_premium = ?, " +
					"broken_discount = ?, " +
					"broken_allowance = ?, " +					
					"broken_premium = ?, " +
					
					"blk_brk_discount = ?, " +
					"blk_brk_allowance = ?, " +					
					"blk_brk_premium = ?, " +
					
					"brown_discount = ?, " +
					"brown_allowance = ?, " +
					"brown_premium = ?, " +
					
					"excelsa_discount = ?, " +
					"excelsa_allowance = ?, " +
					"excelsa_premium = ?, " +
					
					"moldy_discount = ?, " +
					"moldy_allowance = ?, " +
					"moldy_premium = ?, " +
					
					"foreign_matter_discount = ?, " +
					"foreign_matter_allowance = ?, " +
					"foreign_matter_premium = ?, " +
					
					"moisture_discount = ?, " +
					"moisture_allowance = ?, " +
					"moisture_premium = ?, " +
					
					"sc18_discount = ?, " +
					"sc18_allowance = ?, " +
					"sc18_premium = ?, " +
					
					"sc16_discount = ?, " +
					"sc16_allowance = ?, " +
					"sc16_premium = ?, " +
					
					"sc14_discount = ?, " +
					"sc14_allowance = ?, " +
					"sc14_premium = ?, " +
					
					"sc13_discount = ?, " +
					"sc13_allowance = ?, " +
					"sc13_premium = ?, " +
					
					"sc12_discount = ?, " +
					"sc12_allowance = ?, " +
					"sc12_premium = ?, " +
					
					"below_sc12_discount = ?, " +
					"below_sc12_allowance = ?, " +
					"below_sc12_premium = ? " +
					
					"where contract_id = ?";
        		
        			Vector vp = new Vector();
        			vp.addElement(new LongValue(id));
        			vp.addElement(new IntValue(GradeId));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("black_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("black_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("black_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("broken_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("broken_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("broken_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("blk_brk_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("blk_brk_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("blk_brk_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("brown_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("brown_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("brown_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("excelsa_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("excelsa_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("excelsa_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("moldy_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("moldy_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("moldy_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("foreign_matter_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("foreign_matter_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("foreign_matter_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("moisture_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("moisture_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("moisture_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("sc18_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc18_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc18_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("sc16_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc16_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc16_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("sc14_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc14_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc14_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("sc13_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc13_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc13_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("sc12_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc12_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc12_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("below_sc12_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("below_sc12_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("below_sc12_premium")));
        			
        			vp.addElement(new LongValue(id));
        			ret = getSQLCommand().executeUpdate(ISql,vp);
        		}
        		else
        		{
        			ISql = "insert contract_discount_allowance_details " +
					"SET contract_id = ?, " +
					"grade_master_id = ?, " +
					"black_discount = ?, " +
					"black_allowance = ?, " +
					"black_premium = ?, " +
					
					"broken_discount = ?, " +
					"broken_allowance = ?, " +
					"broken_premium = ?, " +
					
					"blk_brk_discount = ?, " +
					"blk_brk_allowance = ?, " +
					"blk_brk_premium = ?, " +
					
					"brown_discount = ?, " +
					"brown_allowance = ?, " +
					"brown_premium = ?, " +
					
					"excelsa_discount = ?, " +
					"excelsa_allowance = ?, " +
					"excelsa_premium = ?, " +
					
					"moldy_discount = ?, " +
					"moldy_allowance = ?, " +
					"moldy_premium = ?, " +
					
					"foreign_matter_discount = ?, " +
					"foreign_matter_allowance = ?, " +
					"foreign_matter_premium = ?, " +
					
					"moisture_discount = ?, " +
					"moisture_allowance = ?, " +
					"moisture_premium = ?, " +
					
					"sc18_discount = ?, " +
					"sc18_allowance = ?, " +
					"sc18_premium = ?, " +
					
					"sc16_discount = ?, " +
					"sc16_allowance = ?, " +
					"sc16_premium = ?, " +
					
					"sc14_discount = ?, " +
					"sc14_allowance = ?, " +
					"sc14_premium = ?, " +
					
					"sc13_discount = ?, " +
					"sc13_allowance = ?, " +
					"sc13_premium = ?, " +
					
					"sc12_discount = ?, " +
					"sc12_allowance = ?, " +
					"sc12_premium = ?, " +
					
					"below_sc12_discount = ?, " +
					"below_sc12_allowance = ?, " +
					"below_sc12_premium = ?";
        			

        			Vector vp = new Vector();
        			vp.addElement(new LongValue(id));
        			vp.addElement(new IntValue(GradeId));
        			
           			vp.addElement(new FloatValue(Rs.getFloat("black_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("black_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("black_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("broken_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("broken_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("broken_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("blk_brk_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("blk_brk_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("blk_brk_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("brown_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("brown_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("brown_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("excelsa_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("excelsa_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("excelsa_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("moldy_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("moldy_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("moldy_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("foreign_matter_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("foreign_matter_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("foreign_matter_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("moisture_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("moisture_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("moisture_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("sc18_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc18_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc18_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("sc16_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc16_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc16_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("sc14_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc14_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc14_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("sc13_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc13_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc13_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("sc12_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc12_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("sc12_premium")));
        			
        			vp.addElement(new FloatValue(Rs.getFloat("below_sc12_discount")));
        			vp.addElement(new FloatValue(Rs.getFloat("below_sc12_allowance")));
        			vp.addElement(new FloatValue(Rs.getFloat("below_sc12_premium")));
 
        			ret = getSQLCommand().executeUpdate(ISql,vp);        		
        		}
        	}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			
			return ret;
        }
        
        private int updatePaymentDetails()
        {
        	int PId = getRequest().getIntValue("id");
        	float DelvyTon = getRequest().getFloatValue("tons_delivered");
        	int CntId = getRequest().getIntValue("contracts");
        	int QrRefNo = getRequest().getIntValue("warehouse_receipt");
        	float PhyDiscPercent = getRequest().getFloatValue("discount_percentage");
        	float PhyDiscVal = getRequest().getFloatValue("physical_discount");
        	float CupTestDisc = getRequest().getFloatValue("cuptest_discount");
        	float StorageDisc = getRequest().getFloatValue("storage_discount");
        	float IntDisc = getRequest().getFloatValue("Interest_discount");
        	float OthDisc = getRequest().getFloatValue("other_discount");
        	float TotDisc = getRequest().getFloatValue("total_discount");
        	float CntPrice = getRequest().getFloatValue("contract_price");
        	float Diff = getRequest().getFloatValue("differential");
        	float MrkPrice = getRequest().getFloatValue("market_price");
        	float Prvprice = getRequest().getFloatValue("provisional_price");
        	float PrvPricePec = getRequest().getFloatValue("provisional_price_percentage");
        	float Exg = getRequest().getFloatValue("exchange_rate");
        	float PrvPrice = getRequest().getFloatValue("provisional_price");
        	String PayType = getRequest().getStringValue("payment_type");
        	float PaidValForeign = getRequest().getFloatValue("amount_paid");
        	
        	String SqlStr = "";
        	int ret =0;
        	
        	if (PId > 0)
        	{
           		SqlStr ="update payment_details " +
	      		"SET cheque_number = ?, " +
	      		"payment_date = ?, " +
	      		"delivered_tons = ?, " +
	      		"contract_id = ?, " +
	      		"qr_id = ?, " +
	      		"physical_discount_percentage = ?, " +
	      		"physical_discount_value = ?, " +
	      		"cuptest_discount = ?, " +
	      		"storage_discount = ?, " +
	      		"interest_discount = ?, " +
	      		"other_discount = ?, " +
	      		"total_discount_value = ?, " +
				"contract_price_foreign = ?, " +
				"differential = ?, " +
				"market_price = ?, " +
				"provisional_price = ?, " +
				"provisional_price_percentage = ?, " +
				"paid_value_foreign = ?, " +
				"paid_value_local = ?, " +
				"exchange_rate = ?, " +
				"payment_type = ? " +
				"where id = ?";	
	        	Vector vp = new Vector();
	        	vp.addElement(new StringValue(""));
	        	vp.addElement(new DateValue(Utils.today()));
	        	vp.addElement(new FloatValue(DelvyTon));
	        	vp.addElement(new IntValue(CntId));
	        	vp.addElement(new IntValue(QrRefNo));
	        	vp.addElement(new FloatValue(PhyDiscPercent));
	        	vp.addElement(new FloatValue(PhyDiscVal));
	        	vp.addElement(new FloatValue(CupTestDisc));
	        	vp.addElement(new FloatValue(StorageDisc));
	        	vp.addElement(new FloatValue(IntDisc));
	        	vp.addElement(new FloatValue(OthDisc));
	        	vp.addElement(new FloatValue(TotDisc));
	        	vp.addElement(new FloatValue(CntPrice));
	        	vp.addElement(new FloatValue(Diff));
	        	vp.addElement(new FloatValue(MrkPrice));
	        	vp.addElement(new FloatValue(PrvPrice));
	        	vp.addElement(new FloatValue(PrvPricePec));
	        	vp.addElement(new FloatValue(PaidValForeign));
	        	vp.addElement(new FloatValue(PaidValForeign*Exg));
	        	vp.addElement(new FloatValue(Exg));
	        	vp.addElement(new StringValue(PayType));
	        	vp.addElement(new IntValue(PId));
	        	ret = getSQLCommand().executeUpdate(SqlStr,vp);	  		
        		
        	}
        	else
        	{
           		SqlStr ="insert payment_details " +
	      		"SET cheque_number = ?, " +
	      		"payment_date = ?, " +
	      		"delivered_tons = ?, " +
	      		"contract_id = ?, " +
	      		"qr_id = ?, " +
	      		"physical_discount_percentage = ?, " +
	      		"physical_discount_value = ?, " +
	      		"cuptest_discount = ?, " +
	      		"storage_discount = ?, " +
	      		"interest_discount = ?, " +
	      		"other_discount = ?, " +
	      		"total_discount_value = ?, " +
				"contract_price_foreign = ?, " +
				"differential = ?, " +
				"market_price = ?, " +
				"provisional_price = ?, " +
				"provisional_price_percentage = ?, " +
				"paid_value_foreign = ?, " +
				"paid_value_local = ?, " +
				"exchange_rate = ?, " +
				"payment_type = ?";

	        	Vector vp = new Vector();
	        	vp.addElement(new StringValue(""));
	        	vp.addElement(new DateValue(Utils.today()));
	        	vp.addElement(new FloatValue(DelvyTon));
	        	vp.addElement(new IntValue(CntId));
	        	vp.addElement(new IntValue(QrRefNo));
	        	vp.addElement(new FloatValue(PhyDiscPercent));
	        	vp.addElement(new FloatValue(PhyDiscVal));
	        	vp.addElement(new FloatValue(CupTestDisc));
	        	vp.addElement(new FloatValue(StorageDisc));
	        	vp.addElement(new FloatValue(IntDisc));
	        	vp.addElement(new FloatValue(OthDisc));
	        	vp.addElement(new FloatValue(TotDisc));
	        	vp.addElement(new FloatValue(CntPrice));
	        	vp.addElement(new FloatValue(Diff));
	        	vp.addElement(new FloatValue(MrkPrice));
	        	vp.addElement(new FloatValue(PrvPrice));
	        	vp.addElement(new FloatValue(PrvPricePec));
	        	vp.addElement(new FloatValue(PaidValForeign));
	        	vp.addElement(new FloatValue(PaidValForeign*Exg));
	        	vp.addElement(new FloatValue(Exg));
	        	vp.addElement(new StringValue(PayType));

	        	ret = getSQLCommand().executeUpdate(SqlStr,vp);	  		
        		
        	}
 
        	return ret;
        }
        
        public float getTotalDiscount()
        {
        	float disc =0;
        	int CntId = getRequest().getIntValue("contracts");
        	String Sql = "Select sum(total_discount_value) as totdisc from payment_details where contract_id = " + CntId;
        	ResultSet Rs = getSQLCommand().executeQuery(Sql);
        	try {
        		Rs.beforeFirst();
        		if (Rs.next()==true)
        		{
        			disc = Rs.getFloat("totdisc");
        		}
        		else
        		{
        			disc = 0;
        		}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	return disc;
        }
        
        public float getTotalPaidAmt()
        {
        	float paid =0;
        	int CntId = getRequest().getIntValue("contracts");
        	String Sql = "Select sum(paid_value_foreign) as totval from payment_details where contract_id = " + CntId;
        	ResultSet Rs = getSQLCommand().executeQuery(Sql);
        	try {
        		Rs.beforeFirst();
        		if (Rs.next()==true)
        		{
        			paid = Rs.getFloat("totval");
        		}
        		else
        		{
        			paid = 0;
        		}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}        	
        	return paid;
        }  
        
		  public ResultSet getContractLsDt()
		  {
		  	return getSQLCommand().executeQuery("select distinct last_date from contracts where last_date IS NOT NULL and active = 1 order by last_date");
		  }		
		  
	        public ResultSet getContractRefList()
	        {
	        	
	        	String Sql="";
	       		Sql ="select id,contract_no,ref_number,tons,receive_tons,unfixed_tons from contracts where active = 1 order by id desc";

	    	  	return getSQLCommand().executeQuery(Sql);        	
	        }        
}


class PrintPurContract extends GenReport
{
	 private Contract purchase = null;
	 
	  public PrintPurContract(Tasks owner)
	  {
	  	super(owner,"PurchaseCnt.xls");
	    this.purchase = (Contract)owner;
	  }
	  
	  public String generate()
	  {
	  	long PcntId=purchase.getRequest().getLongValue("contracts");
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);
  	
	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	String Quality = purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));
		  	
		  	this.set_CellValue(0,4,purchase.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));	
		  	
			this.set_CellValue(8,4,Utils.dateToStr(Rs.getDate("contract_date")));
			
			this.set_CellValue(4,11,Rs.getString("ref_number"));
			
			this.set_CellValue(4,15,Utils.numberToStr(Rs.getFloat("tons"),3)+ " Tons.") ;
			
			this.set_CellValue(4,17,purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"))) ;
			this.set_CellValue(4,18,purchase.getFieldValueForId("grade_master","name",Rs.getInt("grade_id"))) ;
			this.set_CellValue(4,19,Rs.getString("description")) ;
			
			if (Rs.getString("contract_price_type").compareTo("F")==0)
			{
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(4,21,"US $ " + Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Metric Ton") ;
				}
				else
				{
					this.set_CellValue(4,21,Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Cts/Lb") ;
				}
			}
			else
			{
				float DiffVal = Rs.getFloat("differential");
				String DiffStr ="";
				String OptStr ="";
				if (DiffVal > 0)
				{
					DiffStr = "Plus";
				}
				else
				{
					DiffStr = "Minus";
				}
				
				if (Rs.getString("price_fix_option").compareTo("B")==0)
				{
					OptStr = "At Buyer Option.";
				}
				else
				{
					OptStr = "At Seller Option.";
				}
				
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(4,21,"Price to be fixed against " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " at " +  Rs.getString("terminal_market")+ " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential")) + " Usd/Mt." + OptStr);
				}
				else
				{
					this.set_CellValue(4,21,"Price to be fixed against " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " at " +  Rs.getString("terminal_market")+ " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential")) + " Cts/Lb." + OptStr);
				}
			}
			this.set_CellValue(4,23,purchase.getFieldValueForId("contract_term_master","report_term",Rs.getInt("purchase_term_id")));
			
			this.set_CellValue(4,25,"Net Delivered Weight");
			
			this.set_CellValue(4,27,purchase.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")));			
			
			this.set_CellValue(5,29,Utils.dateToStr(Rs.getDate("first_date")));
			this.set_CellValue(7,29,Utils.dateToStr(Rs.getDate("last_date")));
			
			this.set_CellValue(4,31,purchase.getFieldValueForId("contract_term_master","report_term",Rs.getInt("purchase_term_id")));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  
}




class PrintSalContract extends GenReport
{
	 private Contract sales = null;
	 
	  public PrintSalContract(Tasks owner)
	  {
	    super(owner,"SalesCnt.xls");
	    this.sales = (Contract)owner;
	  }
	  
	  public String generate()
	  {
	  	long PcntId=sales.getRequest().getLongValue("contracts");
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);

	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	String Quality = sales.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));
		  	
		  	this.set_CellValue(0,4,sales.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));
		  	
			this.set_CellValue(8,4,Utils.dateToStr(Rs.getDate("contract_date")));
			
			this.set_CellValue(4,11,Rs.getString("ref_number"));
			
			this.set_CellValue(4,15,Utils.numberToStr(Rs.getFloat("tons"),3)+ " Tons.") ;
			
			this.set_CellValue(4,17,sales.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"))) ;
			this.set_CellValue(4,18,sales.getFieldValueForId("grade_master","name",Rs.getInt("grade_id"))) ;
			this.set_CellValue(4,19,Rs.getString("description")) ;
			
			if (Rs.getString("contract_price_type").compareTo("F")==0)
			{
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(4,23,"US $ " + Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Metric Ton") ;
				}
				else
				{
					this.set_CellValue(4,23,Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Cts/Lb") ;
				}
			}
			else
			{
				float DiffVal = Rs.getFloat("differential");
				String DiffStr ="";
				String OptStr ="";
				if (DiffVal > 0)
				{
					DiffStr = "Plus";
				}
				else
				{
					DiffStr = "Minus";
				}
				
				if (Rs.getString("price_fix_option").compareTo("B")==0)
				{
					OptStr = "At Buyer Option.";
				}
				else
				{
					OptStr = "At Seller Option.";
				}
				
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(4,23,"Price to be fixed against " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " at " +  Rs.getString("terminal_market")+ " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential")) + " Usd/Mt." + OptStr);
				}
				else
				{
					this.set_CellValue(4,23,"Price to be fixed against " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " at " +  Rs.getString("terminal_market")+ " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential")) + " Cts/Lb." + OptStr);
				}
			}
			this.set_CellValue(4,25,sales.getFieldValueForId("contract_term_master","report_term",Rs.getInt("sales_term_id")));
			
			this.set_CellValue(4,27,"Net Shipped Weight");
			
			this.set_CellValue(4,29,sales.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")));			

			this.set_CellValue(5,31,Utils.dateToStr(Rs.getDate("first_date")));
			this.set_CellValue(7,31,Utils.dateToStr(Rs.getDate("last_date")));
			
			this.set_CellValue(4,33,sales.getFieldValueForId("country_master","name",Rs.getInt("country_id")));
			
			this.set_CellValue(4,37,sales.getFieldValueForId("arbitration_master","name",Rs.getInt("arbitration_id")));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  	  

}




class PrintMerconContract extends GenReport
{
	 private Contract purchase = null;
	 
	  public PrintMerconContract(Tasks owner)
	  {
	  	super(owner,"Mercon_Contract.xls");
	    this.purchase = (Contract)owner;
	  }
	  

	  public String generate_mercon()
	  {
	  	long PcntId=purchase.getRequest().getLongValue("contracts");
	  	String CntType = purchase.getRequest().getStringValue("type");
	  	
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);
  	
	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	String Quality = purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));
		  	
		  	if (CntType.compareTo("P")==0)
		  	{
		  		this.set_CellValue(0,8,"Seller");
		  		this.set_CellValue(5,8,"Buyer");
		  	}
		  	else
		  	{
		  		this.set_CellValue(0,8,"Buyer");
		  		this.set_CellValue(5,8,"Seller");		  		
		  	}
		  	this.set_CellValue(0,9,purchase.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));
		  	this.set_CellValue(0,10,purchase.getFieldValueForId("company_master","address1",Rs.getInt("supplier_buyer_id")));
		  	this.set_CellValue(0,13,purchase.getFieldValueForId("company_master","address2",Rs.getInt("supplier_buyer_id")));
		  	
			this.set_CellValue(0,6,Rs.getDate("contract_date"));
			
			this.set_CellValue(6,6,Rs.getString("contract_no"));
			
			//this.set_CellValue(4,17,Utils.numberToStr(Rs.getFloat("tons"),3)+ " metric tons") ;
			this.set_CellValue(4,17,Utils.numberToStr(Rs.getFloat("tons"),3)+ " metric tons ( " + Rs.getFloat("no_of_bags")+ " " + purchase.getFieldValueForId("packing_master","name",Rs.getInt("packing_id"))+ " ). Averaging " + Rs.getFloat("kg_per_bag") + " kg each.") ;
			
			
			this.set_CellValue(4,19,purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id")) + "." + purchase.getFieldValueForId("grade_master","name",Rs.getInt("grade_id")) + ".") ;
			
			if (Rs.getString("type").compareTo("P") ==0) this.set_CellValue(4,20,Rs.getString("description")+ "Clean cup. Free from fermented,moldy,smoky and phenol/chemical characteristic.") ;
			if (Rs.getString("type").compareTo("S") ==0) this.set_CellValue(4,20,Rs.getString("description")) ;
			
			if (Rs.getString("contract_price_type").compareTo("F")==0)
			{
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(4,22,"US $ " + Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " / Metric Ton") ;
				}
				else
				{
					this.set_CellValue(4,22,Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " / Cts/Lb") ;
				}
			}
			else
			{
				float DiffVal = Rs.getFloat("differential");
				String DiffStr ="";
				String OptStr ="";
				if (DiffVal > 0)
				{
					DiffStr = "Plus";
				}
				else
				{
					DiffStr = "Minus";
				}
				
				if (Rs.getString("price_fix_option").compareTo("B")==0)
				{
					OptStr = "at Buyer Option.";
				}
				else
				{
					OptStr = "at Seller Option.";
				}
				
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(4,22,"To be fixed " + OptStr + " basis " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + Rs.getString("terminal_market")+ " at " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential")) + " Usd/Mt. (" + Rs.getFloat("no_of_lots") + "lots.");
				}
				else
				{
					this.set_CellValue(4,22,"To be fixed at " + OptStr + " basis " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + Rs.getString("terminal_market")+ " at " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential")) + " Cts/Lb. (" + Rs.getFloat("no_of_lots") + "lots.");
				}
			}
			this.set_CellValue(4,24,purchase.getFieldValueForId("contract_term_master","report_term",Rs.getInt("purchase_term_id")));
			
			this.set_CellValue(4,26,purchase.getFieldValueForId("contract_term_master","weights",Rs.getInt("purchase_term_id")));
			
			this.set_CellValue(4,28,purchase.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")));			
			
			this.set_CellValue(4,30,"100% weighing and sampling by third party nominated by Buyer.");
			this.set_CellValue(4,32,Utils.dateToStr(Rs.getDate("last_date")).substring(3));
			
			this.set_CellValue(4,34,purchase.getFieldValueForId("city_master","name",Rs.getInt("destination_id")));
			
			this.set_CellValue(4,36,purchase.getFieldValueForId("arbitration_master","name",Rs.getInt("arbitration_id")));			
			
			this.set_CellValue(4,38,purchase.getFieldValueForId("contract_term_master","remarks",Rs.getInt("purchase_term_id")));
			
			this.set_CellValue(0,46,Rs.getDate("contract_date"));
			
			this.set_CellValue(6,46,Rs.getString("contract_no"));
			
		  	if (CntType.compareTo("P")==0)
		  	{
		  		this.set_CellValue(0,62,"Seller : ______________________");
		  		this.set_CellValue(5,62,"Buyer : ______________________");
		  	}
		  	else
		  	{
		  		this.set_CellValue(0,62,"Buyer : ______________________");
		  		this.set_CellValue(5,62,"Seller : ______________________");		  		
		  	}			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  
}

class PrintMerconSalesContract extends GenReport
{
	 private Contract purchase = null;
	 
	  public PrintMerconSalesContract(Tasks owner)
	  {
	  	super(owner,"Mercon_Sales_Contract.xls");
	    this.purchase = (Contract)owner;
	  }
	  

	  public String generate_mercon()
	  {
	  	long PcntId=purchase.getRequest().getLongValue("contracts");
	  	String CntType = purchase.getRequest().getStringValue("type");
	  	
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);
  	
	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	String Quality = purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));
		  	
		  	this.set_CellValue(4,12,purchase.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));
		  	this.set_CellValue(4,13,purchase.getFieldValueForId("company_master","address1",Rs.getInt("supplier_buyer_id")));
		  	this.set_CellValue(4,16,purchase.getFieldValueForId("company_master","address2",Rs.getInt("supplier_buyer_id")));
		  	
			this.set_CellValue(6,5,Rs.getDate("contract_date"));
			
			this.set_CellValue(6,6,Rs.getString("contract_no"));
			
			this.set_CellValue(6,7,Rs.getString("supplier_buyer_ref"));
			
			//this.set_CellValue(4,17,Utils.numberToStr(Rs.getFloat("tons"),3)+ " metric tons") ;
			this.set_CellValue(4,19,Utils.numberToStr(Rs.getFloat("tons"),3)+ " metric tons ( " + Rs.getFloat("no_of_bags")+ " " + purchase.getFieldValueForId("packing_master","name",Rs.getInt("packing_id"))+ " ). Averaging " + Rs.getFloat("kg_per_bag") + " kg each.") ;
			
			this.set_CellValue(4,21,purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id")) + "." + purchase.getFieldValueForId("grade_master","name",Rs.getInt("grade_id")) + "." + Rs.getString("description")) ;
			
			this.set_CellValue(4,23,Utils.dateToStr(Rs.getDate("last_date")).substring(3)+ " Shipment") ;
			
			this.set_CellValue(4,25,purchase.getFieldValueForId("city_master","name",Rs.getInt("destination_id")));
			
			if (Rs.getString("contract_price_type").compareTo("F")==0)
			{
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(4,27,"US $ " + Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " / Metric Ton") ;
				}
				else
				{
					this.set_CellValue(4,27,Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " / Cts/Lb") ;
				}
			}
			else
			{
				float DiffVal = Rs.getFloat("differential");
				String DiffStr ="";
				String OptStr ="";
				if (DiffVal > 0)
				{
					DiffStr = "Plus";
				}
				else
				{
					DiffStr = "Minus";
				}
				
				if (Rs.getString("price_fix_option").compareTo("B")==0)
				{
					OptStr = "at Buyer Option.";
				}
				else
				{
					OptStr = "at Seller Option.";
				}
				
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(4,27,"To be fixed " + OptStr + " basis " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + Rs.getString("terminal_market")+ " at " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential")) + " Usd/Mt. (" + Utils.numberToStr(Rs.getFloat("no_of_lots"),0) + "lots.)");
				}
				else
				{
					this.set_CellValue(4,27,"To be fixed at " + OptStr + " basis " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + Rs.getString("terminal_market")+ " at " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential")) + " Cts/Lb. (" + Utils.numberToStr(Rs.getFloat("no_of_lots"),0) + "lots.)");
				}
			}
			this.set_CellValue(4,29,purchase.getFieldValueForId("contract_term_master","report_term",Rs.getInt("purchase_term_id")));
			
			this.set_CellValue(4,31,purchase.getFieldValueForId("contract_term_master","weights",Rs.getInt("purchase_term_id")));
			
			this.set_CellValue(4,33,purchase.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")));			
			
			this.set_CellValue(4,35,purchase.getFieldValueForId("arbitration_master","name",Rs.getInt("arbitration_id")));			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  
}


class PrintVinacofPurContract extends GenReport
{
	 private Contract purchase = null;
	 
	  public PrintVinacofPurContract(Tasks owner)
	  {
	  	super(owner,"Vinacof Purchase Contract.xls");
	    this.purchase = (Contract)owner;
	  }
	  
	  public String generate_vinacof()
	  {
	  	long PcntId=purchase.getRequest().getLongValue("contracts");
	  	String CntPriceType = purchase.getRequest().getStringValue("contract_price_type");
	  	
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);
  	
	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	
		  	String Quality = purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));
		  	String Grade = purchase.getFieldValueForId("grade_master","name",Rs.getInt("grade_id"));
		  	String Desc=Rs.getString("description");
		  	String QGDesc = "Vietnam " + Quality + " - " + Grade + " - " + Desc;
		  	
		  	this.set_CellValue(3,9,Rs.getString("ref_number"));
		  	
		  	this.set_CellValue(1,11,Utils.dateToStr(Rs.getDate("contract_date")));
		  	
		  	this.set_CellValue(1,17,purchase.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));	
		  	//this.set_CellValue(1,18,purchase.getFieldValueForId("company_master","address",Rs.getInt("supplier_buyer_id")));
			
		  	this.set_CellValue(1,23,QGDesc) ;
		  	
		  	String Tons = Utils.numberToStr(Rs.getFloat("tons"),3) + " MT or " + Utils.numberToStr(Rs.getFloat("no_of_bags"),0) + " of 60 Kgs net each.";
			this.set_CellValue(1,30,Tons) ;
			
		  	String Packing = "Packing in " + purchase.getFieldValueForId("packing_master","name",Rs.getInt("packing_id")) + " of 60 Kgs net.";
			this.set_CellValue(1,34,Packing) ;
			
			if (Rs.getString("contract_price_type").compareTo("F")==0)
			{
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(1,37,"US $ " + Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Metric Ton") ;
				}
				else
				{
					this.set_CellValue(1,37,Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Cts/Lb") ;
				}
			}
			else
			{
				float DiffVal = Rs.getFloat("differential");
				String DiffStr ="";
				String OptStr ="";
				if (DiffVal > 0)
				{
					DiffStr = "Plus";
				}
				else
				{
					DiffStr = "Minus";
				}
				
				if (Rs.getString("price_fix_option").compareTo("B")==0)
				{
					OptStr = "At Buyer Option.";
				}
				else
				{
					OptStr = "At Seller Option.";
				}
				
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(1,37,"Price to be fixed against " + Rs.getString("terminal_market") + " " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential"),2) + " Usd/Mt." + OptStr);
				}
				else
				{
					this.set_CellValue(1,37,"Price to be fixed against " + Rs.getString("terminal_market") + " " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential"),2) + " Cts/Lb." + OptStr);
				}
			}
			this.set_CellValue(1,46,purchase.getFieldValueForId("contract_term_master","report_term",Rs.getInt("purchase_term_id")) + " Last delivery date " + Utils.dateToStr(Rs.getDate("last_date")));
			
			String PaytTerm = purchase.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")) + ". " + purchase.getFieldValueForId("payment_term_master","remarks",Rs.getInt("payment_term_id"));
			this.set_CellValue(1,51,PaytTerm);
			
			this.set_CellValue(1,59,purchase.getFieldValueForId("arbitration_master","report_term",Rs.getInt("purchase_term_id")) + " Last delivery date " + Utils.dateToStr(Rs.getDate("last_date")));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  
}


class PrintVinacofUnfixPurContract extends GenReport
{
	 private Contract purchase = null;
	 
	  public PrintVinacofUnfixPurContract(Tasks owner)
	  {
	  	super(owner,"Vinacof Purchase Contract - PTFB.xls");
	    this.purchase = (Contract)owner;
	  }
	  
	  public String generate_vinacof()
	  {
	  	long PcntId=purchase.getRequest().getLongValue("contracts");
	  	String CntPriceType = purchase.getRequest().getStringValue("contract_price_type");
	  	
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);
  	
	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	
		  	String Quality = purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));
		  	String Grade = purchase.getFieldValueForId("grade_master","name",Rs.getInt("grade_id"));
		  	String Desc=Rs.getString("description");
		  	String QGDesc = "Vietnam " + Quality + " - " + Grade + " - " + Desc;
		  	
		  	this.set_CellValue(3,9,Rs.getString("ref_number"));
		  	
		  	this.set_CellValue(1,11,Utils.dateToStr(Rs.getDate("contract_date")));
		  	
		  	this.set_CellValue(1,17,purchase.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));	
		  	//this.set_CellValue(1,18,purchase.getFieldValueForId("company_master","address",Rs.getInt("supplier_buyer_id")));
			
		  	this.set_CellValue(1,23,QGDesc) ;
		  	
		  	String Tons = Utils.numberToStr(Rs.getFloat("tons"),3) + " MT or " + Utils.numberToStr(Rs.getFloat("no_of_bags"),0) + " of 60 Kgs net each.";
			this.set_CellValue(1,30,Tons) ;
			
		  	String Packing = "Packing in " + purchase.getFieldValueForId("packing_master","name",Rs.getInt("packing_id")) + " of 60 Kgs net.";
			this.set_CellValue(1,34,Packing) ;
			
			if (Rs.getString("contract_price_type").compareTo("F")==0)
			{
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(1,37,"US $ " + Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Metric Ton") ;
				}
				else
				{
					this.set_CellValue(1,37,Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Cts/Lb") ;
				}
			}
			else
			{
				float DiffVal = Rs.getFloat("differential");
				String DiffStr ="";
				String OptStr ="";
				if (DiffVal > 0)
				{
					DiffStr = "Plus";
				}
				else
				{
					DiffStr = "Minus";
				}
				
				if (Rs.getString("price_fix_option").compareTo("B")==0)
				{
					OptStr = "At Buyer Option.";
				}
				else
				{
					OptStr = "At Seller Option.";
				}
				
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(1,37,"Price to be fixed against " + Rs.getString("terminal_market") + " " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential"),2) + " Usd/Mt." + OptStr);
				}
				else
				{
					this.set_CellValue(1,37,"Price to be fixed against " + Rs.getString("terminal_market") + " " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential"),2) + " Cts/Lb." + OptStr);
				}
			}
			this.set_CellValue(1,46,purchase.getFieldValueForId("contract_term_master","report_term",Rs.getInt("purchase_term_id")) + " Last delivery date " + Utils.dateToStr(Rs.getDate("last_date")));
			
			String PaytTerm = purchase.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")) + ". " + purchase.getFieldValueForId("payment_term_master","remarks",Rs.getInt("payment_term_id"));
			this.set_CellValue(1,51,PaytTerm);
			
			this.set_CellValue(1,86,purchase.getFieldValueForId("arbitration_master","report_term",Rs.getInt("purchase_term_id")) + " Last delivery date " + Utils.dateToStr(Rs.getDate("last_date")));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  
}


class PrintVinacofSalContract extends GenReport
{
	 private Contract sales = null;
	 
	  public PrintVinacofSalContract(Tasks owner)
	  {
	  	super(owner,"Vinacof Sales Contract.xls");
	    this.sales = (Contract)owner;
	  }
	  
	  public String generate_vinacof()
	  {
	  	long PcntId=sales.getRequest().getLongValue("contracts");
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);
  	
	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	
		  	String Quality = sales.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));
		  	String Grade = sales.getFieldValueForId("grade_master","name",Rs.getInt("grade_id"));
		  	String Desc=Rs.getString("description");
		  	String QGDesc = "Vietnam " + Quality + " - " + Grade + " - " + Desc;
		  	
		  	this.set_CellValue(3,9,Rs.getString("ref_number"));
		  	
		  	this.set_CellValue(1,11,Utils.dateToStr(Rs.getDate("contract_date")));
		  	
		  	this.set_CellValue(1,17,sales.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));	
		  	//this.set_CellValue(1,18,sales.getFieldValueForId("company_master","address",Rs.getInt("supplier_buyer_id")));
			
		  	this.set_CellValue(1,25,Quality + " - " + Grade);
		  	this.set_CellValue(1,27,Desc);
		  	
		  	String Tons = Utils.numberToStr(Rs.getFloat("tons"),3) + " MT or " + Utils.numberToStr(Rs.getFloat("no_of_bags"),0) + " of 60 Kgs net each.";
			this.set_CellValue(1,31,Tons) ;
			
			if (Rs.getString("contract_price_type").compareTo("F")==0)
			{
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(1,33,"US $ " + Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Metric Ton") ;
				}
				else
				{
					this.set_CellValue(1,33,Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Cts/Lb") ;
				}
			}
			else
			{
				float DiffVal = Rs.getFloat("differential");
				String DiffStr ="";
				String OptStr ="";
				if (DiffVal > 0)
				{
					DiffStr = "Plus";
				}
				else
				{
					DiffStr = "Minus";
				}
				
				if (Rs.getString("price_fix_option").compareTo("B")==0)
				{
					OptStr = "At Buyer Option.";
				}
				else
				{
					OptStr = "At Seller Option.";
				}
				
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(1,33,"Price to be fixed against " + Rs.getString("terminal_market") + " " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential"),2) + " Usd/Mt." + OptStr);
				}
				else
				{
					this.set_CellValue(1,33,"Price to be fixed against " + Rs.getString("terminal_market") + " " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential"),2) + " Cts/Lb." + OptStr);
				}
			}
			this.set_CellValue(1,37,sales.getFieldValueForId("contract_term_master","report_term",Rs.getInt("sales_term_id")));
			
			this.set_CellValue(1,39,"Last delivery date " + Utils.dateToStr(Rs.getDate("last_date")) + ". " + sales.getFieldValueForId("packing_master","name",Rs.getInt("packing_id")) );
			
			String PaytTerm = sales.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")) + ". " + sales.getFieldValueForId("payment_term_master","remarks",Rs.getInt("payment_term_id"));
			this.set_CellValue(1,44,PaytTerm);
			
			this.set_CellValue(1,48,sales.getFieldValueForId("arbitration_master","report_term",Rs.getInt("arbitration_id")));
			
			this.set_CellValue(1,50,Rs.getString("remarks"));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  
}


class PrintVinacofUnfixSalContract extends GenReport
{
	 private Contract sales = null;
	 
	  public PrintVinacofUnfixSalContract(Tasks owner)
	  {
	  	super(owner,"Vinacof Sales Contract.xls");
	    this.sales = (Contract)owner;
	  }
	  
	  public String generate_vinacof()
	  {
	  	long PcntId=sales.getRequest().getLongValue("contracts");
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);
  	
	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	
		  	String Quality = sales.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));
		  	String Grade = sales.getFieldValueForId("grade_master","name",Rs.getInt("grade_id"));
		  	String Desc=Rs.getString("description");
		  	String QGDesc = "Vietnam " + Quality + " - " + Grade + " - " + Desc;
		  	
		  	this.set_CellValue(3,9,Rs.getString("ref_number"));
		  	
		  	this.set_CellValue(1,11,Utils.dateToStr(Rs.getDate("contract_date")));
		  	
		  	this.set_CellValue(1,17,sales.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));	
		  	//this.set_CellValue(1,18,sales.getFieldValueForId("company_master","address",Rs.getInt("supplier_buyer_id")));
			
		  	this.set_CellValue(1,25,Quality + " - " + Grade);
		  	this.set_CellValue(1,27,Desc);
		  	
		  	String Tons = Utils.numberToStr(Rs.getFloat("tons"),3) + " MT or " + Utils.numberToStr(Rs.getFloat("no_of_bags"),0) + " of 60 Kgs net each.";
			this.set_CellValue(1,31,Tons) ;
			
			if (Rs.getString("contract_price_type").compareTo("F")==0)
			{
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(1,33,"US $ " + Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Metric Ton") ;
				}
				else
				{
					this.set_CellValue(1,33,Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Cts/Lb") ;
				}
			}
			else
			{
				float DiffVal = Rs.getFloat("differential");
				String DiffStr ="";
				String OptStr ="";
				if (DiffVal > 0)
				{
					DiffStr = "Plus";
				}
				else
				{
					DiffStr = "Minus";
				}
				
				if (Rs.getString("price_fix_option").compareTo("B")==0)
				{
					OptStr = "At Buyer Option.";
				}
				else
				{
					OptStr = "At Seller Option.";
				}
				
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(1,33,"Price to be fixed against " + Rs.getString("terminal_market") + " " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential"),2) + " Usd/Mt." + OptStr);
				}
				else
				{
					this.set_CellValue(1,33,"Price to be fixed against " + Rs.getString("terminal_market") + " " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential"),2) + " Cts/Lb." + OptStr);
				}
			}
			this.set_CellValue(1,37,sales.getFieldValueForId("contract_term_master","report_term",Rs.getInt("sales_term_id")));
			
			this.set_CellValue(1,39,"Last delivery date " + Utils.dateToStr(Rs.getDate("last_date")) + ". " + sales.getFieldValueForId("packing_master","name",Rs.getInt("packing_id")) );
			
			String FutExg = Rs.getString("terminal_market");
			String PT=sales.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")) + ". " + sales.getFieldValueForId("payment_term_master","remarks",Rs.getInt("payment_term_id"));
			String TerMth = Utils.dateToStr(Rs.getDate("terminal_month")).substring(3);
			String LsDt = Utils.dateToStr(Rs.getDate("last_date"));
			String PaytTerm = "1) If shipment is done prior to fixation, a provisional invoice valuing maximum 70% of the contract's provisional. value based on the " + FutExg + " , s closing on the date of B/L will be issued. This provisional amount will be paid to the seller by " + PT + " within 48 hours after the buyer' representative's receipt of the original shipping documents, which are to conform to contractual terms and conditions and the buyers shipping instructions. " + 
								  "A 'Stop Loss' will be put in place on the market. The value of this stop loss level equals 75% of the contract's provisional value plus the discount to " + FutExg + " . " + FutExg + " " +  TerMth + " reaches the stop loss level, fixation will be automatically done." ;
			this.set_CellValue(1,44,PaytTerm);
			PaytTerm = "2) If fixation is done after shipment, the balance amount will be paid against the seller's final invoice within 48 hours from its first presentation. If fixation is done prior to shipment, the contract's unit price will be the average of all fixations minus the differential level.";
			this.set_CellValue(1,45,PaytTerm);
			PaytTerm = "3) Fixation to be done after delivery but not to exceed delivered volume. If the seller has not fixed the price, 30 minutes before the closing of " + FutExg + " on " + LsDt + " , fixation will be done automatically.";
			this.set_CellValue(1,46,PaytTerm);				
			
			this.set_CellValue(1,48,sales.getFieldValueForId("arbitration_master","report_term",Rs.getInt("arbitration_id")));
			
			this.set_CellValue(1,50,Rs.getString("remarks"));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  
}


class PrintNobleContract extends GenReport
{
	 private Contract purchase = null;
	 
	  public PrintNobleContract(Tasks owner)
	  {
	  	super(owner,"Noble_Contract.xls");
	    this.purchase = (Contract)owner;
	  }
	  
	  public String generate_noble()
	  {
	  	long PcntId=purchase.getRequest().getLongValue("contracts");
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);
  	
	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	String Quality = purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));

			this.set_CellValue(4,3,Rs.getString("ref_number"));

			this.set_CellValue(4,4,Utils.dateToStr(Rs.getDate("contract_date")));
			
		  	this.set_CellValue(4,12,purchase.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));	
		  	this.set_CellValue(4,13,purchase.getFieldValueForId("company_master","address1",Rs.getInt("supplier_buyer_id")));
		  	this.set_CellValue(4,14,purchase.getFieldValueForId("company_master","address2",Rs.getInt("supplier_buyer_id")));  	

			this.set_CellValue(4,19,purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"))) ;
			this.set_CellValue(4,20,purchase.getFieldValueForId("grade_master","name",Rs.getInt("grade_id"))) ;
			this.set_CellValue(4,21,Rs.getString("description")) ;
			
			this.set_CellValue(4,23,Utils.numberToStr(Rs.getFloat("tons"),3)+ " Tons.") ;
			
			this.set_CellValue(4,25,purchase.getFieldValueForId("packing_master","name",Rs.getInt("packing_id")));
			
			String FixDt = Utils.dateToStr(Utils.previousDate(Rs.getDate("terminal_month"))).substring(3);
			if (Rs.getString("contract_price_type").compareTo("F")==0)
			{
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(4,27,"US $ " + Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Metric Ton") ;
				}
				else
				{
					this.set_CellValue(4,27,Utils.numberToStr(Rs.getFloat("contract_price_foreign"),2) + " Cts/Lb") ;
				}
			}
			else
			{
				float DiffVal = Rs.getFloat("differential");
				String DiffStr ="";
				String OptStr ="";
				if (DiffVal > 0)
				{
					DiffStr = "Plus";
				}
				else
				{
					DiffStr = "Minus";
				}
				
				if (Rs.getString("price_fix_option").compareTo("B")==0)
				{
					OptStr = "At Buyer Option.";
				}
				else
				{
					OptStr = "At Seller Option.";
				}
				
				if (Quality.compareTo("Robusta")==0)
				{
					this.set_CellValue(4,27,"Price to be fixed against " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " at " +  Rs.getString("terminal_market")+ " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential")) + " Usd/Mt." + OptStr + "Fixation Should Be Fixed Before Last Working Day of " + FixDt);
				}
				else
				{
					this.set_CellValue(4,27,"Price to be fixed against " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " at " +  Rs.getString("terminal_market")+ " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential")) + " Cts/Lb." + OptStr + "Fixation Should Be Fixed Before Last Working Day of " + FixDt);
				}
			}
			
			this.set_CellValue(4,29,purchase.getFieldValueForId("contract_term_master","report_term",Rs.getInt("purchase_term_id")));

			this.set_CellValue(4,31,purchase.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")));

			this.set_CellValue(5,33,Utils.dateToStr(Rs.getDate("first_date")));
			this.set_CellValue(7,33,Utils.dateToStr(Rs.getDate("last_date")));
			
			this.set_CellValue(4,35,purchase.getFieldValueForId("country_master","name",Rs.getInt("country_id")));
			
			this.set_CellValue(4,37,purchase.getFieldValueForId("arbitration_master","name",Rs.getInt("arbitration_id")));
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  
}


class PrintMercafeContract extends GenReport
{
	 private Contract purchase = null;
	  public PrintMercafeContract(Tasks owner)
	  {
	  	super(owner,"Mercafe_PurchaseContract.xls");
	    this.purchase = (Contract)owner;
	  }
	  
	  public String generate_Mercafe_FixPurCnt()
	  {
	  	long PcntId=purchase.getRequest().getLongValue("contracts");
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);
  	
	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	String Quality = purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));

			this.set_CellValue(2,13,Rs.getString("contract_no"));

			this.set_CellValue(5,12,Utils.dateToStr(Rs.getDate("contract_date")));
			
		  	this.set_CellValue(2,23,purchase.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));	
		  	this.set_CellValue(2,24,purchase.getFieldValueForId("company_master","address1",Rs.getInt("supplier_buyer_id")));
		  	this.set_CellValue(2,27,purchase.getFieldValueForId("company_master","address2",Rs.getInt("supplier_buyer_id")));  	

			this.set_CellValue(2,30,purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"))) ;
			this.set_CellValue(2,31,purchase.getFieldValueForId("grade_master","name",Rs.getInt("grade_id"))) ;
			this.set_CellValue(2,32,Rs.getString("description")) ;
			
			String StrBags = "( " + Utils.numberToStr(Rs.getFloat("no_of_bags"),0) + " Bags ). Average 60 Kgs/Bags.";
			this.set_CellValue(2,34,Utils.numberToStr(Rs.getFloat("tons"),3)+ " MetricTons, " + StrBags) ;
		
			String FixDt = Utils.dateToStr(Utils.previousDate(Rs.getDate("terminal_month"))).substring(3);
			this.set_CellValue(2,36,Utils.numberToStr(Rs.getFloat("contract_price_local"),2) + " Dong / Metric Ton");
			
			double CVal = (Rs.getFloat("contract_price_local")*Rs.getFloat("tons"));
			double VAT = (Rs.getFloat("contract_price_local")*Rs.getFloat("tons"))*0.05;
			String TotVal = Utils.numberToStr(CVal + VAT,2);	
			this.set_CellValue(2,38,Utils.numberToStr((VAT),2)+ " Dong/Metric Ton") ;
			
			this.set_CellValue(2,40,TotVal + " Dong");

			this.set_CellValue(3,43,Utils.dateToStr(Rs.getDate("first_date")));
			this.set_CellValue(5,43,Utils.dateToStr(Rs.getDate("last_date")));
			
			this.set_CellValue(4,45,purchase.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")));
			
			String Remarks = "This Contract is executed in two (02) Vietnamese versions and two (02) English versions. Vietnamese shall be used as the ruling language in case there is any discrepancy in interpretation between the two languages. Each Parties keeps one (01) Vietnamese version and one (01) English version." + Rs.getString("remarks");
			this.set_CellValue(2,47,Remarks);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  
}

class PrintMercafeUnfixedContract extends GenReport
{
	 private Contract purchase = null;
	  public PrintMercafeUnfixedContract(Tasks owner)
	  {
	  	super(owner,"Mercafe_Unfixed_purchaseContract.xls");
	    this.purchase = (Contract)owner;
	  }
	  
	  public String generate_Mercafe_UnFixPurCnt()
	  {
	  	long PcntId=purchase.getRequest().getLongValue("contracts");
	  	ResultSet Rs = null;
	  	Rs = getSQLCommand().executeQuery("select * from contracts where id = " + PcntId);
  	
	  	try {
		  	Rs.beforeFirst();
		  	Rs.next();
		  	String Quality = purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"));

			this.set_CellValue(2,13,Rs.getString("contract_no"));

			this.set_CellValue(5,12,Utils.dateToStr(Rs.getDate("contract_date")));
			
		  	this.set_CellValue(2,23,purchase.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));	
		  	this.set_CellValue(2,24,purchase.getFieldValueForId("company_master","address1",Rs.getInt("supplier_buyer_id")));
		  	this.set_CellValue(2,27,purchase.getFieldValueForId("company_master","address2",Rs.getInt("supplier_buyer_id")));  	

			this.set_CellValue(2,30,purchase.getFieldValueForId("quality_master","name",Rs.getInt("quality_id"))) ;
			this.set_CellValue(2,31,purchase.getFieldValueForId("grade_master","name",Rs.getInt("grade_id"))) ;
			this.set_CellValue(2,32,Rs.getString("description")) ;
			
			String StrBags = "( " + Utils.numberToStr(Rs.getFloat("no_of_bags"),0) + " Bags ). Average 60 Kgs/Bags.";
			this.set_CellValue(2,34,Utils.numberToStr(Rs.getFloat("tons"),3)+ " MetricTons, " + StrBags) ;
			
			String FixDt = Utils.dateToStr(Utils.previousDate(Rs.getDate("terminal_month"))).substring(3);
			float DiffVal = Rs.getFloat("differential");
			String DiffStr ="";
			String OptStr ="";
			if (DiffVal > 0)
			{
				DiffStr = "Plus";
			}
			else
			{
				DiffStr = "Minus";
			}
			
			if (Rs.getString("price_fix_option").compareTo("B")==0)
			{
				OptStr = "At Buyer Option.";
			}
			else
			{
				OptStr = "At Seller Option.";
			}
			
			if (Quality.compareTo("Robusta")==0)
			{
				this.set_CellValue(2,36,"Price to be fixed against " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " at " +  Rs.getString("terminal_market")+ " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential"),0) + " Usd/Mt." + OptStr + "Fixation Should Be done Before Last Working Day of " + FixDt);
			}
			else
			{
				this.set_CellValue(2,36,"Price to be fixed against " + Utils.dateToStr(Rs.getDate("terminal_month")).substring(3) + " at " +  Rs.getString("terminal_market")+ " " + DiffStr + " " + Utils.numberToStr(Rs.getFloat("differential"),0) + " Cts/Lb." + OptStr + "Fixation Should Be done Before Last Working Day of " + FixDt);
			}
			
			this.set_CellValue(2,38,Utils.numberToStr((Rs.getFloat("provisional_price")*1000),2)+ " Dong/Metric Ton") ;
			
			double CVal = ((Rs.getFloat("provisional_price")*1000)*Rs.getFloat("tons"));
			double VAT = ((Rs.getFloat("provisional_price")*1000))*0.05;
			String TotVal = Utils.numberToStr(CVal + VAT,2);	
			this.set_CellValue(2,41,Utils.numberToStr((VAT),2)+ " Dong/Metric Ton") ;
			
			this.set_CellValue(2,43,TotVal + " Dong");

			this.set_CellValue(3,46,Utils.dateToStr(Rs.getDate("first_date")));
			this.set_CellValue(5,46,Utils.dateToStr(Rs.getDate("last_date")));
			
			this.set_CellValue(2,48,purchase.getFieldValueForId("payment_term_master","report_term",Rs.getInt("payment_term_id")));
			
			String Remarks = "This Contract is executed in two (02) Vietnamese versions and two (02) English versions. Vietnamese shall be used as the ruling language in case there is any discrepancy in interpretation between the two languages. Each Parties keeps one (01) Vietnamese version and one (01) English version." + Rs.getString("remarks");
			this.set_CellValue(2,50,Remarks);			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();	
	  }	  
}

class PrintMercafePriceConfirmationFixed extends GenReport
{
	  private Contract purchase = null;
	  public PrintMercafePriceConfirmationFixed(Tasks owner)
	  {
	  	super(owner,"MercafePriceCinfirmation-Fixed.xls");
	    this.purchase = (Contract)owner;
	  }
	  
	  public String generate_Mercafe_PriceConfirmationFixed()
	  {
	  	long focused_id=this.getRequest().getIntValue("contracts");
		ResultSet rc   = getSQLCommand().executeQuery("select * from contracts where id = " + focused_id);
		ResultSet rcQR = purchase.getWhseReceiptQualityReport();
		ResultSet rcC = purchase.getContractDiscAllowDetails();	
		ResultSet rcWR = purchase.getWhseReceiptDetails();		
		ResultSet rcP = purchase.getPaymentDetails();
		
		try {
			this.setZeroBlank(false);
			rc.beforeFirst();
			rcQR.beforeFirst();
			rcC.beforeFirst();
			rcWR.beforeFirst();
			rcP.beforeFirst();
			if ((rc.next()==true) && (rcQR.next()==true) && (rcC.next()==true) && (rcWR.next()==true) && (rcP.next()==true))
			{
			this.set_CellValue(4,17,rc.getString("ref_number"));
			this.set_CellValue(4,18,rc.getFloat("tons"));
			this.set_CellValue(4,19,rc.getFloat("receive_tons"));
			
			String Quality = purchase.getFieldValueForId("quality_master","name",rc.getInt("quality_id"));
			String Grade = purchase.getFieldValueForId("grade_master","name",rc.getInt("grade_id"));
			this.set_CellValue(4,20,Quality + " - " + Grade);
			
			this.set_CellValue(4,21,rcWR.getDate("date"));
			this.set_CellValue(4,22,rc.getFloat("contract_price_foreign") * rcP.getFloat("exchange_rate"));

			this.set_CellValue(1,28,rcC.getFloat("moisture_allowance"));
			this.set_CellValue(1,29,rcC.getFloat("blk_brk_allowance"));
			this.set_CellValue(1,30,rcC.getFloat("foreign_matter_allowance"));
			this.set_CellValue(1,31,rcC.getFloat("excelsa_allowance"));
			this.set_CellValue(1,32,rcC.getFloat("moldy_allowance"));
			
			this.set_CellValue(2,28,(rcQR.getFloat("moisture")*1));
			this.set_CellValue(2,29,rcQR.getFloat("black_broken"));
			this.set_CellValue(2,30,rcQR.getFloat("foreign_matter"));
			this.set_CellValue(2,31,rcQR.getFloat("excelsa"));
			this.set_CellValue(2,32,rcQR.getFloat("moldy"));

			this.set_CellFormula(4,28,"IF((C29-B29)<0,0,C29-B29)");
			this.set_CellFormula(4,29,"IF((C30-B30)<0,0,C30-B30)");
			this.set_CellFormula(4,30,"IF((C31-B31)<0,0,C31-B31)");
			this.set_CellFormula(4,31,"IF((C32-B32)<0,0,C32-B32)");
			this.set_CellFormula(4,32,"IF((C33-B33)<=0.2,0,(C33-B33-0.2))");
			
			this.set_CellValue(5,28,rcC.getFloat("moisture_discount")/100);
			this.set_CellValue(5,29,rcC.getFloat("blk_brk_discount")/100);
			this.set_CellValue(5,30,rcC.getFloat("foreign_matter_discount")/100);
			this.set_CellValue(5,31,rcC.getFloat("excelsa_discount")/100);
			this.set_CellValue(5,32,rcC.getFloat("moldy_discount")/100);

			this.set_CellFormula(6,28,"-$E29*$F29");
			this.set_CellFormula(6,29,"-$E30*$F30");
			this.set_CellFormula(6,30,"-$E31*$F31");
			this.set_CellFormula(6,31,"-$E32*$F32");
			this.set_CellFormula(6,32,"-$E33*$F33");
			
			if (rc.getInt("grade_id")==18)
			{
				this.set_CellValue(0,34,"-Above Scr13");
				this.set_CellValue(0,35,"-Below Scr13");
				
				float AbvSc13_cr = rcC.getFloat("sc13_allowance") + rcC.getFloat("sc14_allowance") + rcC.getFloat("sc16_allowance") + rcC.getFloat("sc18_allowance");
				float BelowSc13_cr = rcC.getFloat("sc12_allowance") + rcC.getFloat("below_sc12_allowance");
				this.set_CellValue(1,34,AbvSc13_cr);
				this.set_CellValue(1,35,BelowSc13_cr);				
				
				float AbvSc13_qr = rcQR.getFloat("sc13") + rcQR.getFloat("sc14") + rcQR.getFloat("sc15") + rcQR.getFloat("sc16") + rcQR.getFloat("sc17") + rcQR.getFloat("sc18") + rcQR.getFloat("sc19") + rcQR.getFloat("sc20");
				float BelowSc13_qr = rcQR.getFloat("sc12") + rcQR.getFloat("below_sc12");
				this.set_CellValue(2,34,AbvSc13_qr);
				this.set_CellValue(2,35,BelowSc13_qr);

				this.set_CellFormula(4,34,"IF((C35-B35)>0,0,B35-C35)");
				this.set_CellFormula(4,35,"IF((C36-B36)<0,0,C36-B36)");

				this.set_CellValue(5,34,rcC.getFloat("sc13_discount")/100);
				this.set_CellValue(5,35,rcC.getFloat("sc12_discount")/100);
				
				this.set_CellFormula(6,34,"-$E35*$F35");
				this.set_CellFormula(6,35,"-$E36*$F36");				
			}
			
			if (rc.getInt("grade_id")==9)
			{
				this.set_CellValue(0,34,"-Above Scr16");
				this.set_CellValue(0,35,"-Above Scr13");
				
				float AbvSc16_cr = rcC.getFloat("sc16_allowance") + rcC.getFloat("sc18_allowance");
				float AbvSc13_cr = rcC.getFloat("sc13_allowance") + rcC.getFloat("sc14_allowance");
				this.set_CellValue(1,34,AbvSc16_cr);
				this.set_CellValue(1,35,AbvSc13_cr);				
				
				float AbvSc16_qr = rcQR.getFloat("sc16") + rcQR.getFloat("sc17") + rcQR.getFloat("sc18") + rcQR.getFloat("sc19") + rcQR.getFloat("sc20");
				float AbvSc13_qr = rcQR.getFloat("sc13") + rcQR.getFloat("sc14") + rcQR.getFloat("sc15");
				this.set_CellValue(2,34,AbvSc16_qr);
				this.set_CellValue(2,35,AbvSc13_qr);

				this.set_CellFormula(4,34,"IF((C35-B35)>0,0,B35-C35)");
				this.set_CellFormula(4,35,"IF((C36-B36)<0,0,C36-B36)");

				this.set_CellValue(5,34,rcC.getFloat("sc16_discount")/100);
				this.set_CellValue(5,35,rcC.getFloat("sc13_discount")/100);
				
				this.set_CellFormula(6,34,"-$E35*$F35");
				this.set_CellFormula(6,35,"-$E36*$F36");				
			}			
			
			
			if (rc.getInt("grade_id")==12)
			{
				this.set_CellValue(0,34,"-Above Scr18");
				this.set_CellValue(0,35,"-Above Scr14");
				
				float AbvSc18_cr = rcC.getFloat("sc18_allowance");
				float AbvSc14_cr = rcC.getFloat("sc14_allowance") + rcC.getFloat("sc16_allowance");
				this.set_CellValue(1,34,AbvSc18_cr);
				this.set_CellValue(1,35,AbvSc14_cr);				
				
				float AbvSc18_qr = rcQR.getFloat("sc18") + rcQR.getFloat("sc19") + rcQR.getFloat("sc20");
				float AbvSc14_qr = rcQR.getFloat("sc14") + rcQR.getFloat("sc15") + rcQR.getFloat("sc16") + rcQR.getFloat("sc17");
				this.set_CellValue(2,34,AbvSc18_qr);
				this.set_CellValue(2,35,AbvSc14_qr);

				this.set_CellFormula(4,34,"IF((C35-B35)>0,0,B35-C35)");
				this.set_CellFormula(4,35,"IF((C36-B36)<0,0,C36-B36)");

				this.set_CellValue(5,34,rcC.getFloat("sc18_discount")/100);
				this.set_CellValue(5,35,rcC.getFloat("sc14_discount")/100);
				
				this.set_CellFormula(6,34,"-$E35*$F35");
				this.set_CellFormula(6,35,"-$E36*$F36");				
			}			
			
			this.set_CellFormula(6,36,"SUM(G29:G36)");
			this.set_CellValue(6,37,rc.getFloat("contract_price_foreign")* rcP.getFloat("exchange_rate"));
			this.set_CellFormula(6,38,"(G38*G37)/100");
			this.set_CellFormula(6,39,"SUM(G38:G39)");
			this.set_CellValue(6,40,rcWR.getFloat("net_weight")/1000);
			this.set_CellFormula(6,41,"G40*G41");
			
			this.set_CellValue(6,42,rcP.getFloat("cuptest_discount")*-1);
			this.set_CellValue(6,43,rcP.getFloat("storage_discount")*-1);
			this.set_CellValue(6,44,rcP.getFloat("interest_discount")*-1);
			this.set_CellValue(6,45,rcP.getFloat("other_discount")*-1);
			this.set_CellFormula(6,46,"SUM(G42:G46)");
			}
			rc.close();
			rcQR.close();
			rcC.close();
			rcWR.close();
			rcP.close();			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();
	  }
}


class PrintMercafePriceConfirmationUnFixedPart extends GenReport
{
	  private Contract purchase = null;
	  public PrintMercafePriceConfirmationUnFixedPart(Tasks owner)
	  {
	  	super(owner,"MercafePriceCinfirmation-UF-Part.xls");
	    this.purchase = (Contract)owner;
	  }
	  
	  public String generate_Mercafe_PriceConfirmationUFPart()
	  {
	  	long focused_id=this.getRequest().getIntValue("contracts");
		ResultSet rc   = getSQLCommand().executeQuery("select * from contracts where id = " + focused_id);
		ResultSet rcQR = purchase.getWhseReceiptQualityReport();	
		ResultSet rcC = purchase.getContractDiscAllowDetails();	
		ResultSet rcWR = purchase.getWhseReceiptDetails();		
		ResultSet rcP = purchase.getPaymentDetails();
		
		try {
			this.setZeroBlank(false);
			rc.beforeFirst();
			rcQR.beforeFirst();
			rcC.beforeFirst();
			rcWR.beforeFirst();
			rcP.beforeFirst();
			
			if ((rc.next()==true) && (rcQR.next()==true) && (rcC.next()==true) && (rcWR.next()==true) && (rcP.next()==true))
			{
			this.set_CellValue(5,27,rc.getString("ref_number"));
			this.set_CellValue(5,28,rc.getFloat("tons"));
			this.set_CellValue(5,29,rc.getFloat("receive_tons"));
			
			String Quality = purchase.getFieldValueForId("quality_master","name",rc.getInt("quality_id"));
			String Grade = purchase.getFieldValueForId("grade_master","name",rc.getInt("grade_id"));
			this.set_CellValue(5,30,Quality + " - " + Grade);
			
			this.set_CellValue(5,31,rcWR.getDate("date"));
			this.set_CellValue(5,32,rc.getFloat("contract_price_foreign")* rcP.getFloat("exchange_rate"));

			this.set_CellValue(7,36,rcP.getFloat("market_price"));
			this.set_CellValue(7,37,rcP.getFloat("differential"));
			
			float pp = ((rcP.getFloat("market_price") + rcP.getFloat("differential"))* rcP.getFloat("provisional_price_percentage"))/100;
			this.set_CellValue(7,38,pp);
			this.set_CellValue(7,39,rcP.getFloat("exchange_rate"));
			this.set_CellValue(7,40,rcP.getFloat("exchange_rate")*pp);
			

			this.set_CellValue(1,42,rcC.getFloat("moisture_allowance"));
			this.set_CellValue(1,43,rcC.getFloat("blk_brk_allowance"));
			this.set_CellValue(1,44,rcC.getFloat("foreign_matter_allowance"));
			this.set_CellValue(1,45,rcC.getFloat("excelsa_allowance"));
			this.set_CellValue(1,46,rcC.getFloat("moldy_allowance"));
			
			this.set_CellValue(2,42,(rcQR.getFloat("moisture")*1));
			this.set_CellValue(2,43,rcQR.getFloat("black_broken"));
			this.set_CellValue(2,44,rcQR.getFloat("foreign_matter"));
			this.set_CellValue(2,45,rcQR.getFloat("excelsa"));
			this.set_CellValue(2,46,rcQR.getFloat("moldy"));

			this.set_CellFormula(3,42,"IF((C43-B43)<0,0,C43-B43)");
			this.set_CellFormula(3,43,"IF((C44-B44)<0,0,C44-B44)");
			this.set_CellFormula(3,44,"IF((C45-B45)<0,0,C45-B45)");
			this.set_CellFormula(3,45,"IF((C46-B46)<0,0,C46-B46)");
			this.set_CellFormula(3,46,"IF((C47-B47)<=0.2,0,(C47-B47-0.2))");
			
			this.set_CellValue(5,42,rcC.getFloat("moisture_discount")/100);
			this.set_CellValue(5,43,rcC.getFloat("blk_brk_discount")/100);
			this.set_CellValue(5,44,rcC.getFloat("foreign_matter_discount")/100);
			this.set_CellValue(5,45,rcC.getFloat("excelsa_discount")/100);
			this.set_CellValue(5,46,rcC.getFloat("moldy_discount")/100);

			this.set_CellFormula(7,42,"-$D43*$F43");
			this.set_CellFormula(7,43,"-$D44*$F44");
			this.set_CellFormula(7,44,"-$D45*$F45");
			this.set_CellFormula(7,45,"-$D46*$F46");
			this.set_CellFormula(7,46,"-$D47*$F47");
			
			if (rc.getInt("grade_id")==18)
			{
				this.set_CellValue(0,48,"-Above Scr13");
				this.set_CellValue(0,49,"-Below Scr13");
				
				float AbvSc13_cr = rcC.getFloat("sc13_allowance") + rcC.getFloat("sc14_allowance") + rcC.getFloat("sc16_allowance") + rcC.getFloat("sc18_allowance");
				float BelowSc13_cr = rcC.getFloat("sc12_allowance") + rcC.getFloat("below_sc12_allowance");
				this.set_CellValue(1,48,AbvSc13_cr);
				this.set_CellValue(1,49,BelowSc13_cr);				
				
				float AbvSc13_qr = rcQR.getFloat("sc13") + rcQR.getFloat("sc14") + rcQR.getFloat("sc15") + rcQR.getFloat("sc16") + rcQR.getFloat("sc17") + rcQR.getFloat("sc18") + rcQR.getFloat("sc19") + rcQR.getFloat("sc20");
				float BelowSc13_qr = rcQR.getFloat("sc12") + rcQR.getFloat("below_sc12");
				this.set_CellValue(2,48,AbvSc13_qr);
				this.set_CellValue(2,49,BelowSc13_qr);

				this.set_CellFormula(3,48,"IF((C49-B49)>0,0,B49-C49)");
				this.set_CellFormula(3,49,"IF((C50-B50)<0,0,C50-B50)");

				this.set_CellValue(5,48,rcC.getFloat("sc13_discount")/100);
				this.set_CellValue(5,49,rcC.getFloat("sc12_discount")/100);
				
				this.set_CellFormula(7,48,"-$D49*$F49");
				this.set_CellFormula(7,49,"-$D50*$F50");				
			}
			
			if (rc.getInt("grade_id")==9)
			{
				this.set_CellValue(0,48,"-Above Scr16");
				this.set_CellValue(0,49,"-Above Scr13");
				
				float AbvSc16_cr = rcC.getFloat("sc16_allowance") + rcC.getFloat("sc18_allowance");
				float AbvSc13_cr = rcC.getFloat("sc13_allowance") + rcC.getFloat("sc14_allowance");
				this.set_CellValue(1,48,AbvSc16_cr);
				this.set_CellValue(1,49,AbvSc13_cr);				
				
				float AbvSc16_qr = rcQR.getFloat("sc16") + rcQR.getFloat("sc17") + rcQR.getFloat("sc18") + rcQR.getFloat("sc19") + rcQR.getFloat("sc20");
				float AbvSc13_qr = 100;
				if (rcQR.getFloat("sc12")+rcQR.getFloat("below_sc12")==0)
				{
					AbvSc13_qr = 100;
				}
				else
				{
					AbvSc13_qr = 100-(rcQR.getFloat("sc12")+rcQR.getFloat("below_sc12"));
				}
				
				this.set_CellValue(2,48,AbvSc16_qr);
				this.set_CellValue(2,49,AbvSc13_qr);

				this.set_CellFormula(3,48,"IF((C49-B49)>0,0,B49-C49)");
				this.set_CellFormula(3,49,"IF((C50-B50)<0,0,C50-B50)");

				this.set_CellValue(5,48,rcC.getFloat("sc16_discount")/100);
				this.set_CellValue(5,49,rcC.getFloat("sc13_discount")/100);
				
				this.set_CellFormula(7,48,"-$D49*$F49");
				this.set_CellFormula(7,49,"-$D50*$F50");				
			}			
			
			
			if (rc.getInt("grade_id")==12)
			{
				this.set_CellValue(0,48,"-Above Scr18");
				this.set_CellValue(0,49,"-Above Scr14");
				
				float AbvSc18_cr = rcC.getFloat("sc18_allowance");
				float AbvSc14_cr = rcC.getFloat("sc14_allowance") + rcC.getFloat("sc16_allowance");
				this.set_CellValue(1,48,AbvSc18_cr);
				this.set_CellValue(1,49,AbvSc14_cr);				
				
				float AbvSc18_qr = rcQR.getFloat("sc18") + rcQR.getFloat("sc19") + rcQR.getFloat("sc20");
				float AbvSc14_qr = 100;
				if ((rcQR.getFloat("sc13") + rcQR.getFloat("sc12") + rcQR.getFloat("below_sc12"))==0)
				{
					AbvSc14_qr = 100;
				}
				else
				{
					AbvSc14_qr = 100 - (rcQR.getFloat("sc13") + rcQR.getFloat("sc12") + rcQR.getFloat("below_sc12"));
				}
				this.set_CellValue(2,48,AbvSc18_qr);
				this.set_CellValue(2,49,AbvSc14_qr);

				this.set_CellFormula(3,48,"IF((C49-B49)>0,0,B49-C49)");
				this.set_CellFormula(3,49,"IF((C50-B50)<0,0,C50-B50)");

				this.set_CellValue(5,48,rcC.getFloat("sc18_discount")/100);
				this.set_CellValue(5,49,rcC.getFloat("sc14_discount")/100);
				
				this.set_CellFormula(7,48,"-$D49*$F49");
				this.set_CellFormula(7,49,"-$D50*$F50");				
			}			
			System.out.println("hello");
			this.set_CellFormula(7,50,"SUM(H43:H50)");
			this.set_CellValue(7,51,rcP.getFloat("exchange_rate")*pp);
			this.set_CellFormula(7,52,"(H51*H52)/100");
			this.set_CellFormula(7,53,"SUM(H52:H53)");
			this.set_CellValue(7,54,rcWR.getFloat("net_weight")/1000);
			this.set_CellFormula(7,55,"H54*H55");
			
			this.set_CellValue(7,56,rcP.getFloat("cuptest_discount")*-1);
			this.set_CellValue(7,57,rcP.getFloat("storage_discount")*-1);
			this.set_CellValue(7,58,rcP.getFloat("interest_discount")*-1);
			this.set_CellValue(7,59,rcP.getFloat("other_discount")*-1);
			
			String FormulaPrice = "SUM(H56:H60)";
			this.set_CellFormula(7,61,FormulaPrice);
			}
			rc.close();
			rcQR.close();
			rcC.close();
			rcWR.close();
			rcP.close();			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	  	return export();
	  }
}

class PrintMercafePriceConfirmationUnFixedFinal extends GenReport
{
	  private Contract purchase = null;
	  public PrintMercafePriceConfirmationUnFixedFinal(Tasks owner)
	  {
	  	super(owner,"MercafePriceCinfirmation-UF-Final.xls");
	    this.purchase = (Contract)owner;
	  }
	  
	  public String generate_Mercafe_PriceConfirmationUFFinal()
	  {
	  	long focused_id=this.getRequest().getIntValue("contracts");
		ResultSet rc   = getSQLCommand().executeQuery("select * from contracts where id = " + focused_id);
		ResultSet rcQR = purchase.getWhseReceiptQualityReport();	
		ResultSet rcC = purchase.getContractDiscAllowDetails();	
		ResultSet rcWR = purchase.getWhseReceiptDetails();		
		ResultSet rcP = purchase.getPaymentDetails();
		
		try {
			this.setZeroBlank(false);
			rc.beforeFirst();
			rcQR.beforeFirst();
			rcC.beforeFirst();
			rcWR.beforeFirst();
			rcP.beforeFirst();
			if ((rc.next()==true) && (rcQR.next()==true) && (rcC.next()==true) && (rcWR.next()==true) && (rcP.next()==true))
			{
				System.out.println("Hello");
			this.set_CellValue(5,14,rc.getString("ref_number"));
			this.set_CellValue(5,15,rc.getFloat("tons"));
			this.set_CellValue(5,16,rc.getFloat("receive_tons"));
			
			String Quality = purchase.getFieldValueForId("quality_master","name",rc.getInt("quality_id"));
			String Grade = purchase.getFieldValueForId("grade_master","name",rc.getInt("grade_id"));
			this.set_CellValue(5,17,Quality + " - " + Grade);
			
			this.set_CellValue(5,18,rcWR.getDate("date"));
			
			this.set_CellValue(7,21,rc.getFloat("hedge_price"));
			this.set_CellValue(7,22,rc.getFloat("differential"));
			this.set_CellValue(7,23,rc.getFloat("contract_price_foreign") * rcP.getFloat("exchange_rate"));
			this.set_CellValue(7,24,rcP.getFloat("exchange_rate"));			
			
			this.set_CellValue(1,28,rcC.getFloat("moisture_allowance"));
			this.set_CellValue(1,29,rcC.getFloat("blk_brk_allowance"));
			this.set_CellValue(1,30,rcC.getFloat("foreign_matter_allowance"));
			this.set_CellValue(1,31,rcC.getFloat("excelsa_allowance"));
			this.set_CellValue(1,32,rcC.getFloat("moldy_allowance"));
			
			this.set_CellValue(2,28,(rcQR.getFloat("moisture")*1));
			this.set_CellValue(2,29,rcQR.getFloat("black_broken"));
			this.set_CellValue(2,30,rcQR.getFloat("foreign_matter"));
			this.set_CellValue(2,31,rcQR.getFloat("excelsa"));
			this.set_CellValue(2,32,rcQR.getFloat("moldy"));

			this.set_CellFormula(3,28,"IF((C29-B29)<0,0,C29-B29)");
			this.set_CellFormula(3,29,"IF((C30-B30)<0,0,C30-B30)");
			this.set_CellFormula(3,30,"IF((C31-B31)<0,0,C31-B31)");
			this.set_CellFormula(3,31,"IF((C32-B32)<0,0,C32-B32)");
			this.set_CellFormula(3,32,"IF((C33-B33)<=0.2,0,(C33-B33-0.2))");
			
			this.set_CellValue(5,28,rcC.getFloat("moisture_discount")/100);
			this.set_CellValue(5,29,rcC.getFloat("blk_brk_discount")/100);
			this.set_CellValue(5,30,rcC.getFloat("foreign_matter_discount")/100);
			this.set_CellValue(5,31,rcC.getFloat("excelsa_discount")/100);
			this.set_CellValue(5,32,rcC.getFloat("moldy_discount")/100);

			this.set_CellFormula(7,28,"-$D29*$F29");
			this.set_CellFormula(7,29,"-$D30*$F30");
			this.set_CellFormula(7,30,"-$D31*$F31");
			this.set_CellFormula(7,31,"-$D32*$F32");
			this.set_CellFormula(7,32,"-$D33*$F33");
			
			if (rc.getInt("grade_id")==18)
			{
				this.set_CellValue(0,34,"-Above Scr13");
				this.set_CellValue(0,35,"-Below Scr13");
				
				float AbvSc13_cr = rcC.getFloat("sc13_allowance") + rcC.getFloat("sc14_allowance") + rcC.getFloat("sc16_allowance") + rcC.getFloat("sc18_allowance");
				float BelowSc13_cr = rcC.getFloat("sc12_allowance") + rcC.getFloat("below_sc12_allowance");
				this.set_CellValue(1,34,AbvSc13_cr);
				this.set_CellValue(1,35,BelowSc13_cr);				
				
				float AbvSc13_qr = rcQR.getFloat("sc13") + rcQR.getFloat("sc14") + rcQR.getFloat("sc15") + rcQR.getFloat("sc16") + rcQR.getFloat("sc17") + rcQR.getFloat("sc18") + rcQR.getFloat("sc19") + rcQR.getFloat("sc20");
				float BelowSc13_qr = rcQR.getFloat("sc12") + rcQR.getFloat("below_sc12");
				this.set_CellValue(2,34,AbvSc13_qr);
				this.set_CellValue(2,35,BelowSc13_qr);

				this.set_CellFormula(3,34,"IF((C35-B35)>0,0,B35-C35)");
				this.set_CellFormula(3,35,"IF((C36-B36)<0,0,C36-B36)");

				this.set_CellValue(5,34,rcC.getFloat("sc13_discount")/100);
				this.set_CellValue(5,35,rcC.getFloat("sc12_discount")/100);
				
				this.set_CellFormula(7,34,"-$D35*$F35");
				this.set_CellFormula(7,35,"-$D36*$F36");				
			}
			
			if (rc.getInt("grade_id")==9)
			{
				this.set_CellValue(0,34,"-Above Scr16");
				this.set_CellValue(0,35,"-Above Scr13");
				
				float AbvSc16_cr = rcC.getFloat("sc16_allowance") + rcC.getFloat("sc18_allowance");
				float AbvSc13_cr = rcC.getFloat("sc13_allowance") + rcC.getFloat("sc14_allowance");
				this.set_CellValue(1,34,AbvSc16_cr);
				this.set_CellValue(1,35,AbvSc13_cr);				
				
				float AbvSc16_qr = rcQR.getFloat("sc16") + rcQR.getFloat("sc17") + rcQR.getFloat("sc18") + rcQR.getFloat("sc19") + rcQR.getFloat("sc20");
				float AbvSc13_qr = rcQR.getFloat("sc13") + rcQR.getFloat("sc14") + rcQR.getFloat("sc15");
				this.set_CellValue(2,34,AbvSc16_qr);
				this.set_CellValue(2,35,AbvSc13_qr);

				this.set_CellFormula(3,34,"IF((C35-B35)>0,0,B35-C35)");
				this.set_CellFormula(3,35,"IF((C36-B36)<0,0,C36-B36)");

				this.set_CellValue(5,34,rcC.getFloat("sc16_discount")/100);
				this.set_CellValue(5,35,rcC.getFloat("sc13_discount")/100);
				
				this.set_CellFormula(7,34,"-$D35*$F35");
				this.set_CellFormula(7,35,"-$D36*$F36");				
			}			
			
			
			if (rc.getInt("grade_id")==12)
			{
				this.set_CellValue(0,34,"-Above Scr18");
				this.set_CellValue(0,35,"-Above Scr14");
				
				float AbvSc18_cr = rcC.getFloat("sc18_allowance");
				float AbvSc14_cr = rcC.getFloat("sc14_allowance") + rcC.getFloat("sc16_allowance");
				this.set_CellValue(1,34,AbvSc18_cr);
				this.set_CellValue(1,35,AbvSc14_cr);				
				
				float AbvSc18_qr = rcQR.getFloat("sc18") + rcQR.getFloat("sc19") + rcQR.getFloat("sc20");
				float AbvSc14_qr = rcQR.getFloat("sc14") + rcQR.getFloat("sc15") + rcQR.getFloat("sc16") + rcQR.getFloat("sc17");
				this.set_CellValue(2,34,AbvSc18_qr);
				this.set_CellValue(2,35,AbvSc14_qr);

				this.set_CellFormula(3,34,"IF((C35-B35)>0,0,B35-C35)");
				this.set_CellFormula(3,35,"IF((C36-B36)<0,0,C36-B36)");

				this.set_CellValue(5,34,rcC.getFloat("sc18_discount")/100);
				this.set_CellValue(5,35,rcC.getFloat("sc14_discount")/100);
				
				this.set_CellFormula(7,34,"-$D35*$F35");
				this.set_CellFormula(7,35,"-$D36*$F36");				
			}			
			
			this.set_CellFormula(7,36,"SUM(H29:H36)");
			this.set_CellValue(7,37,rc.getFloat("contract_price_foreign")* rcP.getFloat("exchange_rate"));
			this.set_CellFormula(7,38,"(H37*H38)/100");
			this.set_CellFormula(7,39,"SUM(H38:H39)");
			this.set_CellValue(7,40,rcWR.getFloat("net_weight")/1000);
			this.set_CellFormula(7,41,"G54*G55");
			
			this.set_CellValue(7,56,rcP.getFloat("cuptest_discount")*-1);
			this.set_CellValue(7,57,rcP.getFloat("storage_discount")*-1);
			this.set_CellValue(7,58,rcP.getFloat("interest_discount")*-1);
			this.set_CellValue(7,59,rcP.getFloat("other_discount")*-1);
			//String FormulaPrice = "SUM(H56:H60) * " + rcP.getFloat("provisional_price_percentage");
			//this.set_CellFormula(7,60,FormulaPrice);
			}
			rc.close();
			rcQR.close();
			rcC.close();
			rcWR.close();
			rcP.close();			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  	return export();
	  }
}